Merge branch 'master' into make-projections-test-more-stable

This commit is contained in:
Nikolai Kochetov 2023-03-02 21:17:28 +01:00 committed by GitHub
commit de8e19149c
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
2328 changed files with 124305 additions and 122760 deletions

View File

@ -209,3 +209,5 @@ CheckOptions:
# Workaround clang-tidy bug: https://github.com/llvm/llvm-project/issues/46097
- key: readability-identifier-naming.TypeTemplateParameterIgnoredRegexp
value: expr-type
- key: cppcoreguidelines-avoid-do-while.IgnoreMacros
value: true

View File

@ -549,7 +549,7 @@ jobs:
docker ps --all --quiet | xargs --no-run-if-empty docker rm -f ||:
sudo rm -fr "$TEMP_PATH"
InstallPackagesTestAarch64:
needs: [BuilderDebRelease]
needs: [BuilderDebAarch64]
runs-on: [self-hosted, style-checker-aarch64]
steps:
- name: Set envs

View File

@ -13,9 +13,11 @@ on: # yamllint disable-line rule:truthy
branches:
- master
paths:
- 'CHANGELOG.md'
- 'README.md'
- 'SECURITY.md'
- 'docker/docs/**'
- 'docs/**'
- 'website/**'
- 'utils/check-style/aspell-ignore/**'
jobs:
CheckLabels:

View File

@ -1,118 +0,0 @@
name: DocsReleaseChecks
env:
# Force the stdout and stderr streams to be unbuffered
PYTHONUNBUFFERED: 1
concurrency:
group: master-release
cancel-in-progress: true
'on':
push:
branches:
- master
paths:
- '.github/**'
- 'docker/docs/release/**'
- 'docs/**'
- 'utils/list-versions/version_date.tsv'
- 'website/**'
- 'utils/check-style/aspell-ignore/**'
workflow_dispatch:
jobs:
DockerHubPushAarch64:
runs-on: [self-hosted, style-checker-aarch64]
steps:
- name: Check out repository code
uses: ClickHouse/checkout@v1
with:
clear-repository: true
- name: Images check
run: |
cd "$GITHUB_WORKSPACE/tests/ci"
python3 docker_images_check.py --suffix aarch64
- name: Upload images files to artifacts
uses: actions/upload-artifact@v3
with:
name: changed_images_aarch64
path: ${{ runner.temp }}/docker_images_check/changed_images_aarch64.json
DockerHubPushAmd64:
runs-on: [self-hosted, style-checker]
steps:
- name: Check out repository code
uses: ClickHouse/checkout@v1
with:
clear-repository: true
- name: Images check
run: |
cd "$GITHUB_WORKSPACE/tests/ci"
python3 docker_images_check.py --suffix amd64
- name: Upload images files to artifacts
uses: actions/upload-artifact@v3
with:
name: changed_images_amd64
path: ${{ runner.temp }}/docker_images_check/changed_images_amd64.json
DockerHubPush:
needs: [DockerHubPushAmd64, DockerHubPushAarch64]
runs-on: [self-hosted, style-checker]
steps:
- name: Check out repository code
uses: ClickHouse/checkout@v1
with:
clear-repository: true
- name: Download changed aarch64 images
uses: actions/download-artifact@v3
with:
name: changed_images_aarch64
path: ${{ runner.temp }}
- name: Download changed amd64 images
uses: actions/download-artifact@v3
with:
name: changed_images_amd64
path: ${{ runner.temp }}
- name: Images check
run: |
cd "$GITHUB_WORKSPACE/tests/ci"
python3 docker_manifests_merge.py --suffix amd64 --suffix aarch64
- name: Upload images files to artifacts
uses: actions/upload-artifact@v3
with:
name: changed_images
path: ${{ runner.temp }}/changed_images.json
DocsRelease:
needs: DockerHubPush
runs-on: [self-hosted, func-tester]
steps:
- name: Set envs
# https://docs.github.com/en/actions/learn-github-actions/workflow-commands-for-github-actions#multiline-strings
run: |
cat >> "$GITHUB_ENV" << 'EOF'
TEMP_PATH=${{runner.temp}}/docs_release
REPO_COPY=${{runner.temp}}/docs_release/ClickHouse
CLOUDFLARE_TOKEN=${{secrets.CLOUDFLARE}}
ROBOT_CLICKHOUSE_SSH_KEY<<RCSK
${{secrets.ROBOT_CLICKHOUSE_SSH_KEY}}
RCSK
EOF
- name: Check out repository code
uses: ClickHouse/checkout@v1
with:
clear-repository: true
- name: Download changed images
uses: actions/download-artifact@v3
with:
name: changed_images
path: ${{ env.TEMP_PATH }}
- name: Docs Release
run: |
sudo rm -fr "$TEMP_PATH"
mkdir -p "$TEMP_PATH"
cp -r "$GITHUB_WORKSPACE" "$TEMP_PATH"
cd "$REPO_COPY/tests/ci"
python3 docs_release.py
- name: Cleanup
if: always()
run: |
docker ps --quiet | xargs --no-run-if-empty docker kill ||:
docker ps --all --quiet | xargs --no-run-if-empty docker rm -f ||:
sudo rm -fr "$TEMP_PATH"

View File

@ -983,7 +983,7 @@ jobs:
docker ps --all --quiet | xargs --no-run-if-empty docker rm -f ||:
sudo rm -fr "$TEMP_PATH"
InstallPackagesTestAarch64:
needs: [BuilderDebRelease]
needs: [BuilderDebAarch64]
runs-on: [self-hosted, style-checker-aarch64]
steps:
- name: Set envs

View File

@ -107,7 +107,7 @@ jobs:
run: |
curl --form token="${COVERITY_TOKEN}" \
--form email='security+coverity@clickhouse.com' \
--form file="@$TEMP_PATH/$BUILD_NAME/coverity-scan.tar.zst" \
--form file="@$TEMP_PATH/$BUILD_NAME/coverity-scan.tar.gz" \
--form version="${GITHUB_REF#refs/heads/}-${GITHUB_SHA::6}" \
--form description="Nighly Scan: $(date +'%Y-%m-%dT%H:%M:%S')" \
https://scan.coverity.com/builds?project=ClickHouse%2FClickHouse
@ -154,7 +154,7 @@ jobs:
- name: Set Up Build Tools
run: |
sudo apt-get update
sudo apt-get install -yq git cmake ccache python3 ninja-build
sudo apt-get install -yq git cmake ccache ninja-build python3 yasm
sudo bash -c "$(wget -O - https://apt.llvm.org/llvm.sh)"
- name: Run build-wrapper
run: |

View File

@ -13,9 +13,11 @@ on: # yamllint disable-line rule:truthy
branches:
- master
paths-ignore:
- 'CHANGELOG.md'
- 'README.md'
- 'SECURITY.md'
- 'docker/docs/**'
- 'docs/**'
- 'website/**'
- 'utils/check-style/aspell-ignore/**'
##########################################################################################
##################################### SMALL CHECKS #######################################
@ -1021,7 +1023,7 @@ jobs:
docker ps --all --quiet | xargs --no-run-if-empty docker rm -f ||:
sudo rm -fr "$TEMP_PATH"
InstallPackagesTestAarch64:
needs: [BuilderDebRelease]
needs: [BuilderDebAarch64]
runs-on: [self-hosted, style-checker-aarch64]
steps:
- name: Set envs
@ -3103,10 +3105,10 @@ jobs:
- name: Set envs
run: |
cat >> "$GITHUB_ENV" << 'EOF'
TEMP_PATH=${{runner.temp}}/stress_thread
TEMP_PATH=${{runner.temp}}/stress_asan
REPORTS_PATH=${{runner.temp}}/reports_dir
CHECK_NAME=Stress test (asan)
REPO_COPY=${{runner.temp}}/stress_thread/ClickHouse
REPO_COPY=${{runner.temp}}/stress_asan/ClickHouse
EOF
- name: Download json reports
uses: actions/download-artifact@v3
@ -3265,6 +3267,142 @@ jobs:
docker ps --quiet | xargs --no-run-if-empty docker kill ||:
docker ps --all --quiet | xargs --no-run-if-empty docker rm -f ||:
sudo rm -fr "$TEMP_PATH"
##############################################################################################
######################################### UPGRADE CHECK ######################################
##############################################################################################
UpgradeCheckAsan:
needs: [BuilderDebAsan]
runs-on: [self-hosted, stress-tester]
steps:
- name: Set envs
run: |
cat >> "$GITHUB_ENV" << 'EOF'
TEMP_PATH=${{runner.temp}}/upgrade_asan
REPORTS_PATH=${{runner.temp}}/reports_dir
CHECK_NAME=Upgrade check (asan)
REPO_COPY=${{runner.temp}}/upgrade_asan/ClickHouse
EOF
- name: Download json reports
uses: actions/download-artifact@v3
with:
path: ${{ env.REPORTS_PATH }}
- name: Check out repository code
uses: ClickHouse/checkout@v1
with:
clear-repository: true
- name: Upgrade check
run: |
sudo rm -fr "$TEMP_PATH"
mkdir -p "$TEMP_PATH"
cp -r "$GITHUB_WORKSPACE" "$TEMP_PATH"
cd "$REPO_COPY/tests/ci"
python3 upgrade_check.py "$CHECK_NAME"
- name: Cleanup
if: always()
run: |
docker ps --quiet | xargs --no-run-if-empty docker kill ||:
docker ps --all --quiet | xargs --no-run-if-empty docker rm -f ||:
sudo rm -fr "$TEMP_PATH"
UpgradeCheckTsan:
needs: [BuilderDebTsan]
# same as for stress test with tsan
runs-on: [self-hosted, func-tester]
steps:
- name: Set envs
run: |
cat >> "$GITHUB_ENV" << 'EOF'
TEMP_PATH=${{runner.temp}}/upgrade_thread
REPORTS_PATH=${{runner.temp}}/reports_dir
CHECK_NAME=Upgrade check (tsan)
REPO_COPY=${{runner.temp}}/upgrade_thread/ClickHouse
EOF
- name: Download json reports
uses: actions/download-artifact@v3
with:
path: ${{ env.REPORTS_PATH }}
- name: Check out repository code
uses: ClickHouse/checkout@v1
with:
clear-repository: true
- name: Upgrade check
run: |
sudo rm -fr "$TEMP_PATH"
mkdir -p "$TEMP_PATH"
cp -r "$GITHUB_WORKSPACE" "$TEMP_PATH"
cd "$REPO_COPY/tests/ci"
python3 upgrade_check.py "$CHECK_NAME"
- name: Cleanup
if: always()
run: |
docker ps --quiet | xargs --no-run-if-empty docker kill ||:
docker ps --all --quiet | xargs --no-run-if-empty docker rm -f ||:
sudo rm -fr "$TEMP_PATH"
UpgradeCheckMsan:
needs: [BuilderDebMsan]
runs-on: [self-hosted, stress-tester]
steps:
- name: Set envs
run: |
cat >> "$GITHUB_ENV" << 'EOF'
TEMP_PATH=${{runner.temp}}/upgrade_memory
REPORTS_PATH=${{runner.temp}}/reports_dir
CHECK_NAME=Upgrade check (msan)
REPO_COPY=${{runner.temp}}/upgrade_memory/ClickHouse
EOF
- name: Download json reports
uses: actions/download-artifact@v3
with:
path: ${{ env.REPORTS_PATH }}
- name: Check out repository code
uses: ClickHouse/checkout@v1
with:
clear-repository: true
- name: Upgrade check
run: |
sudo rm -fr "$TEMP_PATH"
mkdir -p "$TEMP_PATH"
cp -r "$GITHUB_WORKSPACE" "$TEMP_PATH"
cd "$REPO_COPY/tests/ci"
python3 upgrade_check.py "$CHECK_NAME"
- name: Cleanup
if: always()
run: |
docker ps --quiet | xargs --no-run-if-empty docker kill ||:
docker ps --all --quiet | xargs --no-run-if-empty docker rm -f ||:
sudo rm -fr "$TEMP_PATH"
UpgradeCheckDebug:
needs: [BuilderDebDebug]
runs-on: [self-hosted, stress-tester]
steps:
- name: Set envs
run: |
cat >> "$GITHUB_ENV" << 'EOF'
TEMP_PATH=${{runner.temp}}/upgrade_debug
REPORTS_PATH=${{runner.temp}}/reports_dir
CHECK_NAME=Upgrade check (debug)
REPO_COPY=${{runner.temp}}/upgrade_debug/ClickHouse
EOF
- name: Download json reports
uses: actions/download-artifact@v3
with:
path: ${{ env.REPORTS_PATH }}
- name: Check out repository code
uses: ClickHouse/checkout@v1
with:
clear-repository: true
- name: Upgrade check
run: |
sudo rm -fr "$TEMP_PATH"
mkdir -p "$TEMP_PATH"
cp -r "$GITHUB_WORKSPACE" "$TEMP_PATH"
cd "$REPO_COPY/tests/ci"
python3 upgrade_check.py "$CHECK_NAME"
- name: Cleanup
if: always()
run: |
docker ps --quiet | xargs --no-run-if-empty docker kill ||:
docker ps --all --quiet | xargs --no-run-if-empty docker rm -f ||:
sudo rm -fr "$TEMP_PATH"
##############################################################################################
##################################### AST FUZZERS ############################################
##############################################################################################

View File

@ -641,7 +641,7 @@ jobs:
docker ps --all --quiet | xargs --no-run-if-empty docker rm -f ||:
sudo rm -fr "$TEMP_PATH"
InstallPackagesTestAarch64:
needs: [BuilderDebRelease]
needs: [BuilderDebAarch64]
runs-on: [self-hosted, style-checker-aarch64]
steps:
- name: Set envs

1
.gitignore vendored
View File

@ -161,6 +161,7 @@ website/package-lock.json
tests/queries/0_stateless/test_*
tests/queries/0_stateless/*.binary
tests/queries/0_stateless/*.generated-expect
tests/queries/0_stateless/*.expect.history
# rust
/rust/**/target

3
.gitmodules vendored
View File

@ -296,6 +296,9 @@
[submodule "contrib/libdivide"]
path = contrib/libdivide
url = https://github.com/ridiculousfish/libdivide
[submodule "contrib/ulid-c"]
path = contrib/ulid-c
url = https://github.com/ClickHouse/ulid-c.git
[submodule "contrib/aws-crt-cpp"]
path = contrib/aws-crt-cpp
url = https://github.com/ClickHouse/aws-crt-cpp

View File

@ -1,9 +1,192 @@
### Table of Contents
**[ClickHouse release v23.2, 2023-02-23](#232)**<br/>
**[ClickHouse release v23.1, 2023-01-25](#231)**<br/>
**[Changelog for 2022](https://clickhouse.com/docs/en/whats-new/changelog/2022/)**<br/>
# 2023 Changelog
### <a id="232"></a> ClickHouse release 23.2, 2023-02-23
#### Backward Incompatible Change
* Extend function "toDayOfWeek()" (alias: "DAYOFWEEK") with a mode argument that encodes whether the week starts on Monday or Sunday and whether counting starts at 0 or 1. For consistency with other date time functions, the mode argument was inserted between the time and the time zone arguments. This breaks existing usage of the (previously undocumented) 2-argument syntax "toDayOfWeek(time, time_zone)". A fix is to rewrite the function into "toDayOfWeek(time, 0, time_zone)". [#45233](https://github.com/ClickHouse/ClickHouse/pull/45233) ([Robert Schulze](https://github.com/rschu1ze)).
* Rename setting `max_query_cache_size` to `filesystem_cache_max_download_size`. [#45614](https://github.com/ClickHouse/ClickHouse/pull/45614) ([Kseniia Sumarokova](https://github.com/kssenii)).
* The `default` user will not have permissions for access type `SHOW NAMED COLLECTION` by default (e.g. `default` user will no longer be able to grant ALL to other users as it was before, therefore this PR is backward incompatible). [#46010](https://github.com/ClickHouse/ClickHouse/pull/46010) ([Kseniia Sumarokova](https://github.com/kssenii)).
* If the SETTINGS clause is specified before the FORMAT clause, the settings will be applied to formatting as well. [#46003](https://github.com/ClickHouse/ClickHouse/pull/46003) ([Azat Khuzhin](https://github.com/azat)).
* Remove support for setting `materialized_postgresql_allow_automatic_update` (which was by default turned off). [#46106](https://github.com/ClickHouse/ClickHouse/pull/46106) ([Kseniia Sumarokova](https://github.com/kssenii)).
* Slightly improve performance of `countDigits` on realistic datasets. This closed [#44518](https://github.com/ClickHouse/ClickHouse/issues/44518). In previous versions, `countDigits(0)` returned `0`; now it returns `1`, which is more correct, and follows the existing documentation. [#46187](https://github.com/ClickHouse/ClickHouse/pull/46187) ([Alexey Milovidov](https://github.com/alexey-milovidov)).
* Disallow creation of new columns compressed by a combination of codecs "Delta" or "DoubleDelta" followed by codecs "Gorilla" or "FPC". This can be bypassed using setting "allow_suspicious_codecs = true". [#45652](https://github.com/ClickHouse/ClickHouse/pull/45652) ([Robert Schulze](https://github.com/rschu1ze)).
#### New Feature
* Add `StorageIceberg` and table function `iceberg` to access iceberg table store on S3. [#45384](https://github.com/ClickHouse/ClickHouse/pull/45384) ([flynn](https://github.com/ucasfl)).
* Allow configuring storage as `SETTINGS disk = '<disk_name>'` (instead of `storage_policy`) and with explicit disk creation `SETTINGS disk = disk(type=s3, ...)`. [#41976](https://github.com/ClickHouse/ClickHouse/pull/41976) ([Kseniia Sumarokova](https://github.com/kssenii)).
* Expose `ProfileEvents` counters in `system.part_log`. [#38614](https://github.com/ClickHouse/ClickHouse/pull/38614) ([Bharat Nallan](https://github.com/bharatnc)).
* Enrichment of the existing `ReplacingMergeTree` engine to allow duplicate the insertion. It leverages the power of both `ReplacingMergeTree` and `CollapsingMergeTree` in one MergeTree engine. Deleted data are not returned when queried, but not removed from disk neither. [#41005](https://github.com/ClickHouse/ClickHouse/pull/41005) ([youennL-cs](https://github.com/youennL-cs)).
* Add `generateULID` function. Closes [#36536](https://github.com/ClickHouse/ClickHouse/issues/36536). [#44662](https://github.com/ClickHouse/ClickHouse/pull/44662) ([Nikolay Degterinsky](https://github.com/evillique)).
* Add `corrMatrix` aggregate function, calculating each two columns. In addition, since Aggregatefunctions `covarSamp` and `covarPop` are similar to `corr`, I add `covarSampMatrix`, `covarPopMatrix` by the way. @alexey-milovidov closes [#44587](https://github.com/ClickHouse/ClickHouse/issues/44587). [#44680](https://github.com/ClickHouse/ClickHouse/pull/44680) ([FFFFFFFHHHHHHH](https://github.com/FFFFFFFHHHHHHH)).
* Introduce arrayShuffle function for random array permutations. [#45271](https://github.com/ClickHouse/ClickHouse/pull/45271) ([Joanna Hulboj](https://github.com/jh0x)).
* Support types `FIXED_SIZE_BINARY` type in Arrow, `FIXED_LENGTH_BYTE_ARRAY` in `Parquet` and match them to `FixedString`. Add settings `output_format_parquet_fixed_string_as_fixed_byte_array/output_format_arrow_fixed_string_as_fixed_byte_array` to control default output type for FixedString. Closes [#45326](https://github.com/ClickHouse/ClickHouse/issues/45326). [#45340](https://github.com/ClickHouse/ClickHouse/pull/45340) ([Kruglov Pavel](https://github.com/Avogar)).
* Add a new column `last_exception_time` to system.replication_queue. [#45457](https://github.com/ClickHouse/ClickHouse/pull/45457) ([Frank Chen](https://github.com/FrankChen021)).
* Add two new functions which allow for user-defined keys/seeds with SipHash{64,128}. [#45513](https://github.com/ClickHouse/ClickHouse/pull/45513) ([Salvatore Mesoraca](https://github.com/aiven-sal)).
* Allow a three-argument version for table function `format`. close [#45808](https://github.com/ClickHouse/ClickHouse/issues/45808). [#45873](https://github.com/ClickHouse/ClickHouse/pull/45873) ([FFFFFFFHHHHHHH](https://github.com/FFFFFFFHHHHHHH)).
* Add `JodaTime` format support for 'x','w','S'. Refer to https://joda-time.sourceforge.net/apidocs/org/joda/time/format/DateTimeFormat.html. [#46073](https://github.com/ClickHouse/ClickHouse/pull/46073) ([zk_kiger](https://github.com/zk-kiger)).
* Support window function `ntile`. ([lgbo](https://github.com/lgbo-ustc)).
* Add setting `final` to implicitly apply the `FINAL` modifier to every table. [#40945](https://github.com/ClickHouse/ClickHouse/pull/40945) ([Arthur Passos](https://github.com/arthurpassos)).
* Added `arrayPartialSort` and `arrayPartialReverseSort` functions. [#46296](https://github.com/ClickHouse/ClickHouse/pull/46296) ([Joanna Hulboj](https://github.com/jh0x)).
* The new http parameter `client_protocol_version` allows setting a client protocol version for HTTP responses using the Native format. [#40397](https://github.com/ClickHouse/ClickHouse/issues/40397). [#46360](https://github.com/ClickHouse/ClickHouse/pull/46360) ([Geoff Genz](https://github.com/genzgd)).
* Add new function `regexpExtract`, like spark function `REGEXP_EXTRACT` for compatibility. It is similar to the existing function `extract`. [#46469](https://github.com/ClickHouse/ClickHouse/pull/46469) ([李扬](https://github.com/taiyang-li)).
* Add new function `JSONArrayLength`, which returns the number of elements in the outermost JSON array. The function returns NULL if the input JSON string is invalid. [#46631](https://github.com/ClickHouse/ClickHouse/pull/46631) ([李扬](https://github.com/taiyang-li)).
#### Performance Improvement
* The introduced logic works if PREWHERE condition is a conjunction of multiple conditions (cond1 AND cond2 AND ... ). It groups those conditions that require reading the same columns into steps. After each step the corresponding part of the full condition is computed and the result rows might be filtered. This allows to read fewer rows in the next steps thus saving IO bandwidth and doing less computation. This logic is disabled by default for now. It will be enabled by default in one of the future releases once it is known to not have any regressions, so it is highly encouraged to be used for testing. It can be controlled by 2 settings: "enable_multiple_prewhere_read_steps" and "move_all_conditions_to_prewhere". [#46140](https://github.com/ClickHouse/ClickHouse/pull/46140) ([Alexander Gololobov](https://github.com/davenger)).
* An option added to aggregate partitions independently if table partition key and group by key are compatible. Controlled by the setting `allow_aggregate_partitions_independently`. Disabled by default because of limited applicability (please refer to the docs). [#45364](https://github.com/ClickHouse/ClickHouse/pull/45364) ([Nikita Taranov](https://github.com/nickitat)).
* Allow using Vertical merge algorithm with parts in Compact format. This will allow ClickHouse server to use much less memory for background operations. This closes [#46084](https://github.com/ClickHouse/ClickHouse/issues/46084). [#45681](https://github.com/ClickHouse/ClickHouse/pull/45681) [#46282](https://github.com/ClickHouse/ClickHouse/pull/46282) ([Anton Popov](https://github.com/CurtizJ)).
* Optimize `Parquet` reader by using batch reader. [#45878](https://github.com/ClickHouse/ClickHouse/pull/45878) ([LiuNeng](https://github.com/liuneng1994)).
* Add new `local_filesystem_read_method` method `io_uring` based on the asynchronous Linux [io_uring](https://kernel.dk/io_uring.pdf) subsystem, improving read performance almost universally compared to the default `pread` method. [#38456](https://github.com/ClickHouse/ClickHouse/pull/38456) ([Saulius Valatka](https://github.com/sauliusvl)).
* Rewrite aggregate functions with `if` expression as argument when logically equivalent. For example, `avg(if(cond, col, null))` can be rewritten to avgIf(cond, col). It is helpful in performance. [#44730](https://github.com/ClickHouse/ClickHouse/pull/44730) ([李扬](https://github.com/taiyang-li)).
* Improve lower/upper function performance with avx512 instructions. [#37894](https://github.com/ClickHouse/ClickHouse/pull/37894) ([yaqi-zhao](https://github.com/yaqi-zhao)).
* Remove the limitation that on systems with >=32 cores and SMT disabled ClickHouse uses only half of the cores (the case when you disable Hyper Threading in BIOS). [#44973](https://github.com/ClickHouse/ClickHouse/pull/44973) ([Robert Schulze](https://github.com/rschu1ze)).
* Improve performance of function `multiIf` by columnar executing, speed up by 2.3x. [#45296](https://github.com/ClickHouse/ClickHouse/pull/45296) ([李扬](https://github.com/taiyang-li)).
* Add fast path for function `position` when the needle is empty. [#45382](https://github.com/ClickHouse/ClickHouse/pull/45382) ([李扬](https://github.com/taiyang-li)).
* Enable `query_plan_remove_redundant_sorting` optimization by default. Optimization implemented in [#45420](https://github.com/ClickHouse/ClickHouse/issues/45420). [#45567](https://github.com/ClickHouse/ClickHouse/pull/45567) ([Igor Nikonov](https://github.com/devcrafter)).
* Increased HTTP Transfer Encoding chunk size to improve performance of large queries using the HTTP interface. [#45593](https://github.com/ClickHouse/ClickHouse/pull/45593) ([Geoff Genz](https://github.com/genzgd)).
* Fixed performance of short `SELECT` queries that read from tables with large number of `Array`/`Map`/`Nested` columns. [#45630](https://github.com/ClickHouse/ClickHouse/pull/45630) ([Anton Popov](https://github.com/CurtizJ)).
* Improve performance of filtering for big integers and decimal types. [#45949](https://github.com/ClickHouse/ClickHouse/pull/45949) ([李扬](https://github.com/taiyang-li)).
* This change could effectively reduce the overhead of obtaining the filter from ColumnNullable(UInt8) and improve the overall query performance. To evaluate the impact of this change, we adopted TPC-H benchmark but revised the column types from non-nullable to nullable, and we measured the QPS of its queries as the performance indicator. [#45962](https://github.com/ClickHouse/ClickHouse/pull/45962) ([Zhiguo Zhou](https://github.com/ZhiguoZh)).
* Make the `_part` and `_partition_id` virtual column be `LowCardinality(String)` type. Closes [#45964](https://github.com/ClickHouse/ClickHouse/issues/45964). [#45975](https://github.com/ClickHouse/ClickHouse/pull/45975) ([flynn](https://github.com/ucasfl)).
* Improve the performance of Decimal conversion when the scale does not change. [#46095](https://github.com/ClickHouse/ClickHouse/pull/46095) ([Alexey Milovidov](https://github.com/alexey-milovidov)).
* Allow to increase prefetching for read data. [#46168](https://github.com/ClickHouse/ClickHouse/pull/46168) ([Kseniia Sumarokova](https://github.com/kssenii)).
* Rewrite `arrayExists(x -> x = 1, arr)` -> `has(arr, 1)`, which improve performance by 1.34x. [#46188](https://github.com/ClickHouse/ClickHouse/pull/46188) ([李扬](https://github.com/taiyang-li)).
* Fix too big memory usage for vertical merges on non-remote disk. Respect `max_insert_delayed_streams_for_parallel_write` for the remote disk. [#46275](https://github.com/ClickHouse/ClickHouse/pull/46275) ([Nikolai Kochetov](https://github.com/KochetovNicolai)).
* Update zstd to v1.5.4. It has some minor improvements in performance and compression ratio. If you run replicas with different versions of ClickHouse you may see reasonable error messages `Data after merge/mutation is not byte-identical to data on another replicas.` with explanation. These messages are Ok and you should not worry. [#46280](https://github.com/ClickHouse/ClickHouse/pull/46280) ([Raúl Marín](https://github.com/Algunenano)).
* Fix performance degradation caused by [#39737](https://github.com/ClickHouse/ClickHouse/issues/39737). [#46309](https://github.com/ClickHouse/ClickHouse/pull/46309) ([Alexey Milovidov](https://github.com/alexey-milovidov)).
* The `replicas_status` handle will answer quickly even in case of a large replication queue. [#46310](https://github.com/ClickHouse/ClickHouse/pull/46310) ([Alexey Milovidov](https://github.com/alexey-milovidov)).
* Add avx512 support for aggregate function `sum`, function unary arithmetic, function comparison. [#37870](https://github.com/ClickHouse/ClickHouse/pull/37870) ([zhao zhou](https://github.com/zzachimed)).
* Rewrote the code around marks distribution and the overall coordination of the reading in order to achieve the maximum performance improvement. This closes [#34527](https://github.com/ClickHouse/ClickHouse/issues/34527). [#43772](https://github.com/ClickHouse/ClickHouse/pull/43772) ([Nikita Mikhaylov](https://github.com/nikitamikhaylov)).
* Remove redundant DISTINCT clauses in query (subqueries). Implemented on top of query plan. It does similar optimization as `optimize_duplicate_order_by_and_distinct` regarding DISTINCT clauses. Can be enabled via `query_plan_remove_redundant_distinct` setting. Related to [#42648](https://github.com/ClickHouse/ClickHouse/issues/42648). [#44176](https://github.com/ClickHouse/ClickHouse/pull/44176) ([Igor Nikonov](https://github.com/devcrafter)).
* A few query rewrite optimizations: `sumIf(123, cond) -> 123 * countIf(1, cond)`, `sum(if(cond, 123, 0)) -> 123 * countIf(cond)`, `sum(if(cond, 0, 123)) -> 123 * countIf(not(cond))` [#44728](https://github.com/ClickHouse/ClickHouse/pull/44728) ([李扬](https://github.com/taiyang-li)).
* Improved how memory bound merging and aggregation in order on top query plan interact. Previously we fell back to explicit sorting for AIO in some cases when it wasn't actually needed. [#45892](https://github.com/ClickHouse/ClickHouse/pull/45892) ([Nikita Taranov](https://github.com/nickitat)).
* Concurrent merges are scheduled using round-robin by default to ensure fair and starvation-free operation. Previously in heavily overloaded shards, big merges could possibly be starved by smaller merges due to the use of strict priority scheduling. Added `background_merges_mutations_scheduling_policy` server config option to select scheduling algorithm (`round_robin` or `shortest_task_first`). [#46247](https://github.com/ClickHouse/ClickHouse/pull/46247) ([Sergei Trifonov](https://github.com/serxa)).
#### Improvement
* Enable retries for INSERT by default in case of ZooKeeper session loss. We already use it in production. [#46308](https://github.com/ClickHouse/ClickHouse/pull/46308) ([Alexey Milovidov](https://github.com/alexey-milovidov)).
* Add ability to ignore unknown keys in JSON object for named tuples (`input_format_json_ignore_unknown_keys_in_named_tuple`). [#45678](https://github.com/ClickHouse/ClickHouse/pull/45678) ([Azat Khuzhin](https://github.com/azat)).
* Support optimizing the `where` clause with sorting key expression move to `prewhere` for query with `final`. [#38893](https://github.com/ClickHouse/ClickHouse/issues/38893). [#38950](https://github.com/ClickHouse/ClickHouse/pull/38950) ([hexiaoting](https://github.com/hexiaoting)).
* Add new metrics for backups: num_processed_files and processed_files_size described actual number of processed files. [#42244](https://github.com/ClickHouse/ClickHouse/pull/42244) ([Aleksandr](https://github.com/AVMusorin)).
* Added retries on interserver DNS errors. [#43179](https://github.com/ClickHouse/ClickHouse/pull/43179) ([Anton Kozlov](https://github.com/tonickkozlov)).
* Keeper improvement: try preallocating space on the disk to avoid undefined out-of-space issues. Introduce setting `max_log_file_size` for the maximum size of Keeper's Raft log files. [#44370](https://github.com/ClickHouse/ClickHouse/pull/44370) ([Antonio Andelic](https://github.com/antonio2368)).
* Optimize behavior for a replica delay api logic in case the replica is read-only. [#45148](https://github.com/ClickHouse/ClickHouse/pull/45148) ([mateng915](https://github.com/mateng0915)).
* Ask for the password in clickhouse-client interactively in a case when the empty password is wrong. Closes [#46702](https://github.com/ClickHouse/ClickHouse/issues/46702). [#46730](https://github.com/ClickHouse/ClickHouse/pull/46730) ([Nikolay Degterinsky](https://github.com/evillique)).
* Mark `Gorilla` compression on columns of non-Float* type as suspicious. [#45376](https://github.com/ClickHouse/ClickHouse/pull/45376) ([Robert Schulze](https://github.com/rschu1ze)).
* Show replica name that is executing a merge in the `postpone_reason` column. [#45458](https://github.com/ClickHouse/ClickHouse/pull/45458) ([Frank Chen](https://github.com/FrankChen021)).
* Save exception stack trace in part_log. [#45459](https://github.com/ClickHouse/ClickHouse/pull/45459) ([Frank Chen](https://github.com/FrankChen021)).
* The `regexp_tree` dictionary is polished and now it is compatible with https://github.com/ua-parser/uap-core. [#45631](https://github.com/ClickHouse/ClickHouse/pull/45631) ([Han Fei](https://github.com/hanfei1991)).
* Updated checking of `SYSTEM SYNC REPLICA`, resolves [#45508](https://github.com/ClickHouse/ClickHouse/issues/45508) [#45648](https://github.com/ClickHouse/ClickHouse/pull/45648) ([SmitaRKulkarni](https://github.com/SmitaRKulkarni)).
* Rename setting `replication_alter_partitions_sync` to `alter_sync`. [#45659](https://github.com/ClickHouse/ClickHouse/pull/45659) ([Antonio Andelic](https://github.com/antonio2368)).
* The `generateRandom` table function and the engine now support `LowCardinality` data types. This is useful for testing, for example you can write `INSERT INTO table SELECT * FROM generateRandom() LIMIT 1000`. This is needed to debug [#45590](https://github.com/ClickHouse/ClickHouse/issues/45590). [#45661](https://github.com/ClickHouse/ClickHouse/pull/45661) ([Alexey Milovidov](https://github.com/alexey-milovidov)).
* The experimental query result cache now provides more modular configuration settings. [#45679](https://github.com/ClickHouse/ClickHouse/pull/45679) ([Robert Schulze](https://github.com/rschu1ze)).
* Renamed "query result cache" to "query cache". [#45682](https://github.com/ClickHouse/ClickHouse/pull/45682) ([Robert Schulze](https://github.com/rschu1ze)).
* add `SYSTEM SYNC FILE CACHE` command. It will do the `sync` syscall. [#8921](https://github.com/ClickHouse/ClickHouse/issues/8921). [#45685](https://github.com/ClickHouse/ClickHouse/pull/45685) ([DR](https://github.com/freedomDR)).
* Add a new S3 setting `allow_head_object_request`. This PR makes usage of `GetObjectAttributes` request instead of `HeadObject` introduced in https://github.com/ClickHouse/ClickHouse/pull/45288 optional (and disabled by default). [#45701](https://github.com/ClickHouse/ClickHouse/pull/45701) ([Vitaly Baranov](https://github.com/vitlibar)).
* Add ability to override connection settings based on connection names (that said that now you can forget about storing password for each connection, you can simply put everything into `~/.clickhouse-client/config.xml` and even use different history files for them, which can be also useful). [#45715](https://github.com/ClickHouse/ClickHouse/pull/45715) ([Azat Khuzhin](https://github.com/azat)).
* Arrow format: support the duration type. Closes [#45669](https://github.com/ClickHouse/ClickHouse/issues/45669). [#45750](https://github.com/ClickHouse/ClickHouse/pull/45750) ([flynn](https://github.com/ucasfl)).
* Extend the logging in the Query Cache to improve investigations of the caching behavior. [#45751](https://github.com/ClickHouse/ClickHouse/pull/45751) ([Robert Schulze](https://github.com/rschu1ze)).
* The query cache's server-level settings are now reconfigurable at runtime. [#45758](https://github.com/ClickHouse/ClickHouse/pull/45758) ([Robert Schulze](https://github.com/rschu1ze)).
* Hide password in logs when a table function's arguments are specified with a named collection. [#45774](https://github.com/ClickHouse/ClickHouse/pull/45774) ([Vitaly Baranov](https://github.com/vitlibar)).
* Improve internal S3 client to correctly deduce regions and redirections for different types of URLs. [#45783](https://github.com/ClickHouse/ClickHouse/pull/45783) ([Antonio Andelic](https://github.com/antonio2368)).
* Add support for Map, IPv4 and IPv6 types in generateRandom. Mostly useful for testing. [#45785](https://github.com/ClickHouse/ClickHouse/pull/45785) ([Raúl Marín](https://github.com/Algunenano)).
* Support empty/notEmpty for IP types. [#45799](https://github.com/ClickHouse/ClickHouse/pull/45799) ([Yakov Olkhovskiy](https://github.com/yakov-olkhovskiy)).
* The column `num_processed_files` was split into two columns: `num_files` (for BACKUP) and `files_read` (for RESTORE). The column `processed_files_size` was split into two columns: `total_size` (for BACKUP) and `bytes_read` (for RESTORE). [#45800](https://github.com/ClickHouse/ClickHouse/pull/45800) ([Vitaly Baranov](https://github.com/vitlibar)).
* Add support for `SHOW ENGINES` query for MySQL compatibility. [#45859](https://github.com/ClickHouse/ClickHouse/pull/45859) ([Filatenkov Artur](https://github.com/FArthur-cmd)).
* Improved how the obfuscator deals with queries. [#45867](https://github.com/ClickHouse/ClickHouse/pull/45867) ([Raúl Marín](https://github.com/Algunenano)).
* Improve behaviour of conversion into Date for boundary value 65535 (2149-06-06). [#46042](https://github.com/ClickHouse/ClickHouse/pull/46042) [#45914](https://github.com/ClickHouse/ClickHouse/pull/45914) ([Joanna Hulboj](https://github.com/jh0x)).
* Add setting `check_referential_table_dependencies` to check referential dependencies on `DROP TABLE`. This PR solves [#38326](https://github.com/ClickHouse/ClickHouse/issues/38326). [#45936](https://github.com/ClickHouse/ClickHouse/pull/45936) ([Vitaly Baranov](https://github.com/vitlibar)).
* Fix `tupleElement` to return `Null` when having `Null` argument. Closes [#45894](https://github.com/ClickHouse/ClickHouse/issues/45894). [#45952](https://github.com/ClickHouse/ClickHouse/pull/45952) ([flynn](https://github.com/ucasfl)).
* Throw an error on no files satisfying the S3 wildcard. Closes [#45587](https://github.com/ClickHouse/ClickHouse/issues/45587). [#45957](https://github.com/ClickHouse/ClickHouse/pull/45957) ([chen](https://github.com/xiedeyantu)).
* Use cluster state data to check concurrent backup/restore. [#45982](https://github.com/ClickHouse/ClickHouse/pull/45982) ([SmitaRKulkarni](https://github.com/SmitaRKulkarni)).
* ClickHouse Client: Use "exact" matching for fuzzy search, which has correct case ignorance and more appropriate algorithm for matching SQL queries. [#46000](https://github.com/ClickHouse/ClickHouse/pull/46000) ([Azat Khuzhin](https://github.com/azat)).
* Forbid wrong create View syntax `CREATE View X TO Y AS SELECT`. Closes [#4331](https://github.com/ClickHouse/ClickHouse/issues/4331). [#46043](https://github.com/ClickHouse/ClickHouse/pull/46043) ([flynn](https://github.com/ucasfl)).
* Storage `Log` family support setting the `storage_policy`. Closes [#43421](https://github.com/ClickHouse/ClickHouse/issues/43421). [#46044](https://github.com/ClickHouse/ClickHouse/pull/46044) ([flynn](https://github.com/ucasfl)).
* Improve `JSONColumns` format when the result is empty. Closes [#46024](https://github.com/ClickHouse/ClickHouse/issues/46024). [#46053](https://github.com/ClickHouse/ClickHouse/pull/46053) ([flynn](https://github.com/ucasfl)).
* Add reference implementation for SipHash128. [#46065](https://github.com/ClickHouse/ClickHouse/pull/46065) ([Salvatore Mesoraca](https://github.com/aiven-sal)).
* Add a new metric to record allocations times and bytes using mmap. [#46068](https://github.com/ClickHouse/ClickHouse/pull/46068) ([李扬](https://github.com/taiyang-li)).
* Currently for functions like `leftPad`, `rightPad`, `leftPadUTF8`, `rightPadUTF8`, the second argument `length` must be UInt8|16|32|64|128|256. Which is too strict for clickhouse users, besides, it is not consistent with other similar functions like `arrayResize`, `substring` and so on. [#46103](https://github.com/ClickHouse/ClickHouse/pull/46103) ([李扬](https://github.com/taiyang-li)).
* Fix assertion in the `welchTTest` function in debug build when the resulting statistics is NaN. Unified the behavior with other similar functions. Change the behavior of `studentTTest` to return NaN instead of throwing an exception because the previous behavior was inconvenient. This closes [#41176](https://github.com/ClickHouse/ClickHouse/issues/41176) This closes [#42162](https://github.com/ClickHouse/ClickHouse/issues/42162). [#46141](https://github.com/ClickHouse/ClickHouse/pull/46141) ([Alexey Milovidov](https://github.com/alexey-milovidov)).
* More convenient usage of big integers and ORDER BY WITH FILL. Allow using plain integers for start and end points in WITH FILL when ORDER BY big (128-bit and 256-bit) integers. Fix the wrong result for big integers with negative start or end points. This closes [#16733](https://github.com/ClickHouse/ClickHouse/issues/16733). [#46152](https://github.com/ClickHouse/ClickHouse/pull/46152) ([Alexey Milovidov](https://github.com/alexey-milovidov)).
* Add `parts`, `active_parts` and `total_marks` columns to `system.tables` on [issue](https://github.com/ClickHouse/ClickHouse/issues/44336). [#46161](https://github.com/ClickHouse/ClickHouse/pull/46161) ([attack204](https://github.com/attack204)).
* Functions "multi[Fuzzy]Match(Any|AnyIndex|AllIndices}" now reject regexes which will likely evaluate very slowly in vectorscan. [#46167](https://github.com/ClickHouse/ClickHouse/pull/46167) ([Robert Schulze](https://github.com/rschu1ze)).
* When `insert_null_as_default` is enabled and column doesn't have defined default value, the default of column type will be used. Also this PR fixes using default values on nulls in case of LowCardinality columns. [#46171](https://github.com/ClickHouse/ClickHouse/pull/46171) ([Kruglov Pavel](https://github.com/Avogar)).
* Prefer explicitly defined access keys for S3 clients. If `use_environment_credentials` is set to `true`, and the user has provided the access key through query or config, they will be used instead of the ones from the environment variable. [#46191](https://github.com/ClickHouse/ClickHouse/pull/46191) ([Antonio Andelic](https://github.com/antonio2368)).
* Add an alias "DATE_FORMAT()" for function "formatDateTime()" to improve compatibility with MySQL's SQL dialect, extend function `formatDateTime` with substitutions "a", "b", "c", "h", "i", "k", "l" "r", "s", "W". ### Documentation entry for user-facing changes User-readable short description: `DATE_FORMAT` is an alias of `formatDateTime`. Formats a Time according to the given Format string. Format is a constant expression, so you cannot have multiple formats for a single result column. (Provide link to [formatDateTime](https://clickhouse.com/docs/en/sql-reference/functions/date-time-functions/#formatdatetime)). [#46302](https://github.com/ClickHouse/ClickHouse/pull/46302) ([Jake Bamrah](https://github.com/JakeBamrah)).
* Add `ProfileEvents` and `CurrentMetrics` about the callback tasks for parallel replicas (`s3Cluster` and `MergeTree` tables). [#46313](https://github.com/ClickHouse/ClickHouse/pull/46313) ([Alexey Milovidov](https://github.com/alexey-milovidov)).
* Add support for `DELETE` and `UPDATE` for tables using `KeeperMap` storage engine. [#46330](https://github.com/ClickHouse/ClickHouse/pull/46330) ([Antonio Andelic](https://github.com/antonio2368)).
* Allow writing RENAME queries with query parameters. Resolves [#45778](https://github.com/ClickHouse/ClickHouse/issues/45778). [#46407](https://github.com/ClickHouse/ClickHouse/pull/46407) ([Nikolay Degterinsky](https://github.com/evillique)).
* Fix parameterized SELECT queries with REPLACE transformer. Resolves [#33002](https://github.com/ClickHouse/ClickHouse/issues/33002). [#46420](https://github.com/ClickHouse/ClickHouse/pull/46420) ([Nikolay Degterinsky](https://github.com/evillique)).
* Exclude the internal database used for temporary/external tables from the calculation of asynchronous metric "NumberOfDatabases". This makes the behavior consistent with system table "system.databases". [#46435](https://github.com/ClickHouse/ClickHouse/pull/46435) ([Robert Schulze](https://github.com/rschu1ze)).
* Added `last_exception_time` column into distribution_queue table. [#46564](https://github.com/ClickHouse/ClickHouse/pull/46564) ([Aleksandr](https://github.com/AVMusorin)).
* Support for IN clause with parameter in parameterized views. [#46583](https://github.com/ClickHouse/ClickHouse/pull/46583) ([SmitaRKulkarni](https://github.com/SmitaRKulkarni)).
* Do not load named collections on server startup (load them on first access instead). [#46607](https://github.com/ClickHouse/ClickHouse/pull/46607) ([Kseniia Sumarokova](https://github.com/kssenii)).
#### Build/Testing/Packaging Improvement
* Introduce GWP-ASan implemented by the LLVM runtime. This closes [#27039](https://github.com/ClickHouse/ClickHouse/issues/27039). [#45226](https://github.com/ClickHouse/ClickHouse/pull/45226) ([Han Fei](https://github.com/hanfei1991)).
* We want to make our tests less stable and more flaky: add randomization for merge tree settings in tests. [#38983](https://github.com/ClickHouse/ClickHouse/pull/38983) ([Anton Popov](https://github.com/CurtizJ)).
* Enable the HDFS support in PowerPC and which helps to fixes the following functional tests 02113_hdfs_assert.sh, 02244_hdfs_cluster.sql and 02368_cancel_write_into_hdfs.sh. [#44949](https://github.com/ClickHouse/ClickHouse/pull/44949) ([MeenaRenganathan22](https://github.com/MeenaRenganathan22)).
* Add systemd.service file for clickhouse-keeper. Fixes [#44293](https://github.com/ClickHouse/ClickHouse/issues/44293). [#45568](https://github.com/ClickHouse/ClickHouse/pull/45568) ([Mikhail f. Shiryaev](https://github.com/Felixoid)).
* ClickHouse's fork of poco was moved from "contrib/" to "base/poco/". [#46075](https://github.com/ClickHouse/ClickHouse/pull/46075) ([Robert Schulze](https://github.com/rschu1ze)).
* Add an option for `clickhouse-watchdog` to restart the child process. This does not make a lot of use. [#46312](https://github.com/ClickHouse/ClickHouse/pull/46312) ([Alexey Milovidov](https://github.com/alexey-milovidov)).
* If the environment variable `CLICKHOUSE_DOCKER_RESTART_ON_EXIT` is set to 1, the Docker container will run `clickhouse-server` as a child instead of the first process, and restart it when it exited. [#46391](https://github.com/ClickHouse/ClickHouse/pull/46391) ([Alexey Milovidov](https://github.com/alexey-milovidov)).
* Fix Systemd service file. [#46461](https://github.com/ClickHouse/ClickHouse/pull/46461) ([SuperDJY](https://github.com/cmsxbc)).
* Raised the minimum Clang version needed to build ClickHouse from 12 to 15. [#46710](https://github.com/ClickHouse/ClickHouse/pull/46710) ([Robert Schulze](https://github.com/rschu1ze)).
* Upgrade Intel QPL from v0.3.0 to v1.0.0 2. Build libaccel-config and link it statically to QPL library instead of dynamically. [#45809](https://github.com/ClickHouse/ClickHouse/pull/45809) ([jasperzhu](https://github.com/jinjunzh)).
#### Bug Fix (user-visible misbehavior in official stable or prestable release)
* Flush data exactly by `rabbitmq_flush_interval_ms` or by `rabbitmq_max_block_size` in `StorageRabbitMQ`. Closes [#42389](https://github.com/ClickHouse/ClickHouse/issues/42389). Closes [#45160](https://github.com/ClickHouse/ClickHouse/issues/45160). [#44404](https://github.com/ClickHouse/ClickHouse/pull/44404) ([Kseniia Sumarokova](https://github.com/kssenii)).
* Use PODArray to render in sparkBar function, so we can control the memory usage. Close [#44467](https://github.com/ClickHouse/ClickHouse/issues/44467). [#44489](https://github.com/ClickHouse/ClickHouse/pull/44489) ([Duc Canh Le](https://github.com/canhld94)).
* Fix functions (quantilesExactExclusive, quantilesExactInclusive) return unsorted array element. [#45379](https://github.com/ClickHouse/ClickHouse/pull/45379) ([wujunfu](https://github.com/wujunfu)).
* Fix uncaught exception in HTTPHandler when open telemetry is enabled. [#45456](https://github.com/ClickHouse/ClickHouse/pull/45456) ([Frank Chen](https://github.com/FrankChen021)).
* Don't infer Dates from 8 digit numbers. It could lead to wrong data to be read. [#45581](https://github.com/ClickHouse/ClickHouse/pull/45581) ([Kruglov Pavel](https://github.com/Avogar)).
* Fixes to correctly use `odbc_bridge_use_connection_pooling` setting. [#45591](https://github.com/ClickHouse/ClickHouse/pull/45591) ([Bharat Nallan](https://github.com/bharatnc)).
* When the callback in the cache is called, it is possible that this cache is destructed. To keep it safe, we capture members by value. It's also safe for task schedule because it will be deactivated before storage is destroyed. Resolve [#45548](https://github.com/ClickHouse/ClickHouse/issues/45548). [#45601](https://github.com/ClickHouse/ClickHouse/pull/45601) ([Han Fei](https://github.com/hanfei1991)).
* Fix data corruption when codecs Delta or DoubleDelta are combined with codec Gorilla. [#45615](https://github.com/ClickHouse/ClickHouse/pull/45615) ([Robert Schulze](https://github.com/rschu1ze)).
* Correctly check types when using N-gram bloom filter index to avoid invalid reads. [#45617](https://github.com/ClickHouse/ClickHouse/pull/45617) ([Antonio Andelic](https://github.com/antonio2368)).
* A couple of segfaults have been reported around `c-ares`. They were introduced in my previous pull requests. I have fixed them with the help of Alexander Tokmakov. [#45629](https://github.com/ClickHouse/ClickHouse/pull/45629) ([Arthur Passos](https://github.com/arthurpassos)).
* Fix key description when encountering duplicate primary keys. This can happen in projections. See [#45590](https://github.com/ClickHouse/ClickHouse/issues/45590) for details. [#45686](https://github.com/ClickHouse/ClickHouse/pull/45686) ([Amos Bird](https://github.com/amosbird)).
* Set compression method and level for backup Closes [#45690](https://github.com/ClickHouse/ClickHouse/issues/45690). [#45737](https://github.com/ClickHouse/ClickHouse/pull/45737) ([Pradeep Chhetri](https://github.com/chhetripradeep)).
* Should use `select_query_typed.limitByOffset()` instead of `select_query_typed.limitOffset()`. [#45817](https://github.com/ClickHouse/ClickHouse/pull/45817) ([刘陶峰](https://github.com/taofengliu)).
* When use experimental analyzer, queries like `SELECT number FROM numbers(100) LIMIT 10 OFFSET 10;` get wrong results (empty result for this sql). That is caused by an unnecessary offset step added by planner. [#45822](https://github.com/ClickHouse/ClickHouse/pull/45822) ([刘陶峰](https://github.com/taofengliu)).
* Backward compatibility - allow implicit narrowing conversion from UInt64 to IPv4 - required for "INSERT ... VALUES ..." expression. [#45865](https://github.com/ClickHouse/ClickHouse/pull/45865) ([Yakov Olkhovskiy](https://github.com/yakov-olkhovskiy)).
* Bugfix IPv6 parser for mixed ip4 address with missed first octet (like `::.1.2.3`). [#45871](https://github.com/ClickHouse/ClickHouse/pull/45871) ([Yakov Olkhovskiy](https://github.com/yakov-olkhovskiy)).
* Add the `query_kind` column to the `system.processes` table and the `SHOW PROCESSLIST` query. Remove duplicate code. It fixes a bug: the global configuration parameter `max_concurrent_select_queries` was not respected to queries with `INTERSECT` or `EXCEPT` chains. [#45872](https://github.com/ClickHouse/ClickHouse/pull/45872) ([Alexey Milovidov](https://github.com/alexey-milovidov)).
* Fix crash in a function `stochasticLinearRegression`. Found by WingFuzz. [#45985](https://github.com/ClickHouse/ClickHouse/pull/45985) ([Nikolai Kochetov](https://github.com/KochetovNicolai)).
* Fix crash in `SELECT` queries with `INTERSECT` and `EXCEPT` modifiers that read data from tables with enabled sparse columns (controlled by setting `ratio_of_defaults_for_sparse_serialization`). [#45987](https://github.com/ClickHouse/ClickHouse/pull/45987) ([Anton Popov](https://github.com/CurtizJ)).
* Fix read in order optimization for DESC sorting with FINAL, close [#45815](https://github.com/ClickHouse/ClickHouse/issues/45815). [#46009](https://github.com/ClickHouse/ClickHouse/pull/46009) ([Vladimir C](https://github.com/vdimir)).
* Fix reading of non existing nested columns with multiple level in compact parts. [#46045](https://github.com/ClickHouse/ClickHouse/pull/46045) ([Azat Khuzhin](https://github.com/azat)).
* Fix elapsed column in system.processes (10x error). [#46047](https://github.com/ClickHouse/ClickHouse/pull/46047) ([Azat Khuzhin](https://github.com/azat)).
* Follow-up fix for Replace domain IP types (IPv4, IPv6) with native https://github.com/ClickHouse/ClickHouse/pull/43221. [#46087](https://github.com/ClickHouse/ClickHouse/pull/46087) ([Yakov Olkhovskiy](https://github.com/yakov-olkhovskiy)).
* Fix environment variable substitution in the configuration when a parameter already has a value. This closes [#46131](https://github.com/ClickHouse/ClickHouse/issues/46131). This closes [#9547](https://github.com/ClickHouse/ClickHouse/issues/9547). [#46144](https://github.com/ClickHouse/ClickHouse/pull/46144) ([pufit](https://github.com/pufit)).
* Fix incorrect predicate push down with grouping sets. Closes [#45947](https://github.com/ClickHouse/ClickHouse/issues/45947). [#46151](https://github.com/ClickHouse/ClickHouse/pull/46151) ([flynn](https://github.com/ucasfl)).
* Fix possible pipeline stuck error on `fulls_sorting_join` with constant keys. [#46175](https://github.com/ClickHouse/ClickHouse/pull/46175) ([Vladimir C](https://github.com/vdimir)).
* Never rewrite tuple functions as literals during formatting to avoid incorrect results. [#46232](https://github.com/ClickHouse/ClickHouse/pull/46232) ([Salvatore Mesoraca](https://github.com/aiven-sal)).
* Fix possible out of bounds error while reading LowCardinality(Nullable) in Arrow format. [#46270](https://github.com/ClickHouse/ClickHouse/pull/46270) ([Kruglov Pavel](https://github.com/Avogar)).
* Fix `SYSTEM UNFREEZE` queries failing with the exception `CANNOT_PARSE_INPUT_ASSERTION_FAILED`. [#46325](https://github.com/ClickHouse/ClickHouse/pull/46325) ([Aleksei Filatov](https://github.com/aalexfvk)).
* Fix possible crash which can be caused by an integer overflow while deserializing aggregating state of a function that stores HashTable. [#46349](https://github.com/ClickHouse/ClickHouse/pull/46349) ([Nikolai Kochetov](https://github.com/KochetovNicolai)).
* Fix possible `LOGICAL_ERROR` in asynchronous inserts with invalid data sent in format `VALUES`. [#46350](https://github.com/ClickHouse/ClickHouse/pull/46350) ([Anton Popov](https://github.com/CurtizJ)).
* Fixed a LOGICAL_ERROR on an attempt to execute `ALTER ... MOVE PART ... TO TABLE`. This type of query was never actually supported. [#46359](https://github.com/ClickHouse/ClickHouse/pull/46359) ([Alexander Tokmakov](https://github.com/tavplubix)).
* Fix s3Cluster schema inference in parallel distributed insert select when `parallel_distributed_insert_select` is enabled. [#46381](https://github.com/ClickHouse/ClickHouse/pull/46381) ([Kruglov Pavel](https://github.com/Avogar)).
* Fix queries like `ALTER TABLE ... UPDATE nested.arr1 = nested.arr2 ...`, where `arr1` and `arr2` are fields of the same `Nested` column. [#46387](https://github.com/ClickHouse/ClickHouse/pull/46387) ([Anton Popov](https://github.com/CurtizJ)).
* Scheduler may fail to schedule a task. If it happens, the whole MulityPartUpload should be aborted and `UploadHelper` must wait for already scheduled tasks. [#46451](https://github.com/ClickHouse/ClickHouse/pull/46451) ([Dmitry Novik](https://github.com/novikd)).
* Fix PREWHERE for Merge with different default types (fixes some `NOT_FOUND_COLUMN_IN_BLOCK` when the default type for the column differs, also allow `PREWHERE` when the type of column is the same across tables, and prohibit it, only if it differs). [#46454](https://github.com/ClickHouse/ClickHouse/pull/46454) ([Azat Khuzhin](https://github.com/azat)).
* Fix a crash that could happen when constant values are used in `ORDER BY`. Fixes [#46466](https://github.com/ClickHouse/ClickHouse/issues/46466). [#46493](https://github.com/ClickHouse/ClickHouse/pull/46493) ([Nikolai Kochetov](https://github.com/KochetovNicolai)).
* Do not throw exception if `disk` setting was specified on query level, but `storage_policy` was specified in config merge tree settings section. `disk` will override setting from config. [#46533](https://github.com/ClickHouse/ClickHouse/pull/46533) ([Kseniia Sumarokova](https://github.com/kssenii)).
* Fix an invalid processing of constant `LowCardinality` argument in function `arrayMap`. This bug could lead to a segfault in release, and logical error `Bad cast` in debug build. [#46569](https://github.com/ClickHouse/ClickHouse/pull/46569) ([Alexey Milovidov](https://github.com/alexey-milovidov)).
* fixes [#46557](https://github.com/ClickHouse/ClickHouse/issues/46557). [#46611](https://github.com/ClickHouse/ClickHouse/pull/46611) ([Alexander Gololobov](https://github.com/davenger)).
* Fix endless restarts of clickhouse-server systemd unit if server cannot start within 1m30sec (Disable timeout logic for starting clickhouse-server from systemd service). [#46613](https://github.com/ClickHouse/ClickHouse/pull/46613) ([Azat Khuzhin](https://github.com/azat)).
* Allocated during asynchronous inserts memory buffers were deallocated in the global context and MemoryTracker counters for corresponding user and query were not updated correctly. That led to false positive OOM exceptions. [#46622](https://github.com/ClickHouse/ClickHouse/pull/46622) ([Dmitry Novik](https://github.com/novikd)).
* Updated to not clear on_expression from table_join as its used by future analyze runs resolves [#45185](https://github.com/ClickHouse/ClickHouse/issues/45185). [#46487](https://github.com/ClickHouse/ClickHouse/pull/46487) ([SmitaRKulkarni](https://github.com/SmitaRKulkarni)).
### <a id="231"></a> ClickHouse release 23.1, 2023-01-26
### ClickHouse release 23.1

View File

@ -391,10 +391,12 @@ if (COMPILER_CLANG)
set(CMAKE_CXX_FLAGS "${CMAKE_CXX_FLAGS} -fstrict-vtable-pointers")
# Set new experimental pass manager, it's a performance, build time and binary size win.
# Can be removed after https://reviews.llvm.org/D66490 merged and released to at least two versions of clang.
set(CMAKE_CXX_FLAGS "${CMAKE_CXX_FLAGS} -fexperimental-new-pass-manager")
set(CMAKE_C_FLAGS "${CMAKE_C_FLAGS} -fexperimental-new-pass-manager")
if (CMAKE_CXX_COMPILER_VERSION VERSION_LESS 16)
# Set new experimental pass manager, it's a performance, build time and binary size win.
# Can be removed after https://reviews.llvm.org/D66490 merged and released to at least two versions of clang.
set(CMAKE_CXX_FLAGS "${CMAKE_CXX_FLAGS} -fexperimental-new-pass-manager")
set(CMAKE_C_FLAGS "${CMAKE_C_FLAGS} -fexperimental-new-pass-manager")
endif ()
# We cannot afford to use LTO when compiling unit tests, and it's not enough
# to only supply -fno-lto at the final linking stage. So we disable it
@ -458,7 +460,7 @@ endif ()
set (CMAKE_POSTFIX_VARIABLE "CMAKE_${CMAKE_BUILD_TYPE_UC}_POSTFIX")
set (CMAKE_POSITION_INDEPENDENT_CODE OFF)
if (OS_LINUX AND NOT ARCH_AARCH64)
if (OS_LINUX AND NOT (ARCH_AARCH64 OR ARCH_S390X))
# Slightly more efficient code can be generated
# It's disabled for ARM because otherwise ClickHouse cannot run on Android.
set (CMAKE_CXX_FLAGS_RELWITHDEBINFO "${CMAKE_CXX_FLAGS_RELWITHDEBINFO} -fno-pie")

View File

@ -2,6 +2,11 @@
ClickHouse® is an open-source column-oriented database management system that allows generating analytical data reports in real-time.
## How To Install (Linux, macOS, FreeBSD)
```
curl https://clickhouse.com/ | sh
```
## Useful Links
* [Official website](https://clickhouse.com/) has a quick high-level overview of ClickHouse on the main page.
@ -9,14 +14,13 @@ ClickHouse® is an open-source column-oriented database management system that a
* [Tutorial](https://clickhouse.com/docs/en/getting_started/tutorial/) shows how to set up and query a small ClickHouse cluster.
* [Documentation](https://clickhouse.com/docs/en/) provides more in-depth information.
* [YouTube channel](https://www.youtube.com/c/ClickHouseDB) has a lot of content about ClickHouse in video format.
* [Slack](https://clickhousedb.slack.com/) and [Telegram](https://telegram.me/clickhouse_en) allow chatting with ClickHouse users in real-time.
* [Slack](https://join.slack.com/t/clickhousedb/shared_invite/zt-1gh9ds7f4-PgDhJAaF8ad5RbWBAAjzFg) and [Telegram](https://telegram.me/clickhouse_en) allow chatting with ClickHouse users in real-time.
* [Blog](https://clickhouse.com/blog/) contains various ClickHouse-related articles, as well as announcements and reports about events.
* [Code Browser (Woboq)](https://clickhouse.com/codebrowser/ClickHouse/index.html) with syntax highlight and navigation.
* [Code Browser (github.dev)](https://github.dev/ClickHouse/ClickHouse) with syntax highlight, powered by github.dev.
* [Contacts](https://clickhouse.com/company/contact) can help to get your questions answered if there are any.
## Upcoming Events
* [**ClickHouse Workshop**](https://clickhouse.com/company/events/2023-02-15-clickhouse-workshop?utm_source=github&utm_medium=social&utm_campaign=workshop) - Feb 15 & 16 - In this 2-day (3 hrs per day) free training, topics range from introductory content to a deep dive on interacting with and understanding your data. There will be both live training and hands-on labs.
* [**v23.2 Release Webinar**](https://clickhouse.com/company/events/v23-2-release-webinar?utm_source=github&utm_medium=social&utm_campaign=release-webinar-2023-02) - Feb 23 - 23.2 is rapidly approaching. Original creator, co-founder, and CTO of ClickHouse Alexey Milovidov will walk us through the highlights of the release.
* [**ClickHouse Meetup in Amsterdam**](https://www.meetup.com/clickhouse-netherlands-user-group/events/291485868/) - Mar 9 - The first ClickHouse Amsterdam Meetup of 2023 is here! 🎉 Join us for short lightning talks and long discussions. Food, drinks & good times on us.
* [**ClickHouse Meetup in SF Bay Area**](https://www.meetup.com/clickhouse-silicon-valley-meetup-group/events/291490121/) - Mar 14 - A night to meet with ClickHouse team in the San Francisco area! Food and drink are a given...but networking is the primary focus.

View File

@ -13,9 +13,10 @@ The following versions of ClickHouse server are currently being supported with s
| Version | Supported |
|:-|:-|
| 23.2 | ✔️ |
| 23.1 | ✔️ |
| 22.12 | ✔️ |
| 22.11 | ✔️ |
| 22.11 | |
| 22.10 | ❌ |
| 22.9 | ❌ |
| 22.8 | ✔️ |

View File

@ -12,21 +12,22 @@
template <typename To, typename From>
std::decay_t<To> bit_cast(const From & from)
{
/**
* Assume the source value is 0xAABBCCDD (i.e. sizeof(from) == 4).
* Its BE representation is 0xAABBCCDD, the LE representation is 0xDDCCBBAA.
* Further assume, sizeof(res) == 8 and that res is initially zeroed out.
* With LE, the result after bit_cast will be 0xDDCCBBAA00000000 --> input value == output value.
* With BE, the result after bit_cast will be 0x00000000AABBCCDD --> input value == output value.
*/
/** Assume the source value is 0xAABBCCDD (i.e. sizeof(from) == 4).
* Its BE representation is 0xAABBCCDD, the LE representation is 0xDDCCBBAA.
* Further assume, sizeof(res) == 8 and that res is initially zeroed out.
* With LE, the result after bit_cast will be 0xDDCCBBAA00000000 --> input value == output value.
* With BE, the result after bit_cast will be 0x00000000AABBCCDD --> input value == output value.
*/
To res {};
if constexpr (std::endian::native == std::endian::little)
memcpy(static_cast<void*>(&res), &from, std::min(sizeof(res), sizeof(from)));
{
memcpy(static_cast<void*>(&res), &from, std::min(sizeof(res), sizeof(from)));
}
else
{
uint32_t offset_to = (sizeof(res) > sizeof(from)) ? (sizeof(res) - sizeof(from)) : 0;
uint32_t offset_from = (sizeof(from) > sizeof(res)) ? (sizeof(from) - sizeof(res)) : 0;
memcpy(reinterpret_cast<char *>(&res) + offset_to, reinterpret_cast<const char *>(&from) + offset_from, std::min(sizeof(res), sizeof(from)));
uint32_t offset_to = (sizeof(res) > sizeof(from)) ? (sizeof(res) - sizeof(from)) : 0;
uint32_t offset_from = (sizeof(from) > sizeof(res)) ? (sizeof(from) - sizeof(res)) : 0;
memcpy(reinterpret_cast<char *>(&res) + offset_to, reinterpret_cast<const char *>(&from) + offset_from, std::min(sizeof(res), sizeof(from)));
}
return res;
}

View File

@ -13,11 +13,7 @@ using char8_t = unsigned char;
#endif
/// This is needed for more strict aliasing. https://godbolt.org/z/xpJBSb https://stackoverflow.com/a/57453713
#if !defined(PVS_STUDIO) /// But PVS-Studio does not treat it correctly.
using UInt8 = char8_t;
#else
using UInt8 = uint8_t;
#endif
using UInt16 = uint16_t;
using UInt32 = uint32_t;

View File

@ -195,7 +195,6 @@ long splice(int fd_in, off_t *off_in, int fd_out, off_t *off_out, size_t len, un
#include <sys/stat.h>
#include <stdint.h>
#if !defined(__aarch64__)
struct statx {
uint32_t stx_mask;
uint32_t stx_blksize;
@ -226,7 +225,6 @@ int statx(int fd, const char *restrict path, int flag,
{
return syscall(SYS_statx, fd, path, flag, mask, statxbuf);
}
#endif
#include <syscall.h>

View File

@ -8,3 +8,8 @@ int fallocate(int fd, int mode, off_t base, off_t len)
{
return syscall(SYS_fallocate, fd, mode, base, len);
}
int fallocate64(int fd, int mode, off_t base, off_t len)
{
return fallocate(fd, mode, base, len);
}

View File

@ -78,9 +78,6 @@
*
*/
// Disable warnings by PVS-Studio
//-V::GA
static const double
pi = 3.14159265358979311600e+00, /* 0x400921FB, 0x54442D18 */
a0 = 7.72156649015328655494e-02, /* 0x3FB3C467, 0xE37DB0C8 */

View File

@ -85,9 +85,6 @@
*
*/
// Disable warnings by PVS-Studio
//-V::GA
#include <stdint.h>
#include <math.h>
#include "libm.h"

View File

@ -155,7 +155,7 @@ static inline long double fp_barrierl(long double x)
static inline void fp_force_evalf(float x)
{
volatile float y;
y = x; //-V1001
y = x;
}
#endif
@ -164,7 +164,7 @@ static inline void fp_force_evalf(float x)
static inline void fp_force_eval(double x)
{
volatile double y;
y = x; //-V1001
y = x;
}
#endif
@ -173,7 +173,7 @@ static inline void fp_force_eval(double x)
static inline void fp_force_evall(long double x)
{
volatile long double y;
y = x; //-V1001
y = x;
}
#endif

View File

@ -3,9 +3,6 @@
* SPDX-License-Identifier: MIT
*/
// Disable warnings by PVS-Studio
//-V::GA
#include <math.h>
#include <stdint.h>
#include "libm.h"

View File

@ -9,3 +9,8 @@ ssize_t pwritev(int fd, const struct iovec *iov, int count, off_t ofs)
/// There was cancellable syscall (syscall_cp), but I don't care.
return syscall(SYS_pwritev, fd, iov, count, (long)(ofs), (long)(ofs>>32));
}
ssize_t pwritev64(int fd, const struct iovec *iov, int count, off_t ofs)
{
return pwritev(fd, iov, count, ofs);
}

View File

@ -455,7 +455,7 @@ auto bounded_rand(RngType& rng, typename RngType::result_type upper_bound)
typedef typename RngType::result_type rtype;
rtype threshold = (RngType::max() - RngType::min() + rtype(1) - upper_bound)
% upper_bound;
for (;;) { //-V1044
for (;;) {
rtype r = rng() - RngType::min();
if (r >= threshold)
return r % upper_bound;

View File

@ -930,7 +930,7 @@ struct rxs_m_xs_mixin {
constexpr bitcount_t shift = bits - xtypebits;
constexpr bitcount_t mask = (1 << opbits) - 1;
bitcount_t rshift =
opbits ? bitcount_t(internal >> (bits - opbits)) & mask : 0; //-V547
opbits ? bitcount_t(internal >> (bits - opbits)) & mask : 0;
internal ^= internal >> (opbits + rshift);
internal *= mcg_multiplier<itype>::multiplier();
xtype result = internal >> shift;
@ -952,7 +952,7 @@ struct rxs_m_xs_mixin {
internal *= mcg_unmultiplier<itype>::unmultiplier();
bitcount_t rshift = opbits ? (internal >> (bits - opbits)) & mask : 0; //-V547
bitcount_t rshift = opbits ? (internal >> (bits - opbits)) & mask : 0;
internal = unxorshift(internal, bits, opbits + rshift);
return internal;
@ -977,7 +977,7 @@ struct rxs_m_mixin {
: 2;
constexpr bitcount_t shift = bits - xtypebits;
constexpr bitcount_t mask = (1 << opbits) - 1;
bitcount_t rshift = opbits ? (internal >> (bits - opbits)) & mask : 0; //-V547
bitcount_t rshift = opbits ? (internal >> (bits - opbits)) & mask : 0;
internal ^= internal >> (opbits + rshift);
internal *= mcg_multiplier<itype>::multiplier();
xtype result = internal >> shift;
@ -1368,7 +1368,7 @@ void extended<table_pow2,advance_pow2,baseclass,extvalclass,kdd>::selfinit()
// - any strange correlations would only be apparent if we
// were to backstep the generator so that the base generator
// was generating the same values again
result_type xdiff = baseclass::operator()() - baseclass::operator()(); //-V501
result_type xdiff = baseclass::operator()() - baseclass::operator()();
for (size_t i = 0; i < table_size; ++i) {
data_[i] = baseclass::operator()() ^ xdiff;
}

View File

@ -21,6 +21,7 @@ if (ENABLE_SSL)
-Wno-unreachable-code-return
-Wno-unused-parameter
-Wno-zero-as-null-pointer-constant
-Wno-used-but-marked-unused
)
target_include_directories (_poco_crypto SYSTEM PUBLIC "include")
target_link_libraries (_poco_crypto PUBLIC Poco::Foundation OpenSSL::SSL OpenSSL::Crypto)

View File

@ -18,121 +18,124 @@
#define Crypto_Cipher_INCLUDED
#include "Poco/Crypto/Crypto.h"
#include "Poco/RefCountedObject.h"
#include "Poco/AutoPtr.h"
#include <istream>
#include <ostream>
#include <vector>
#include "Poco/AutoPtr.h"
#include "Poco/Crypto/Crypto.h"
#include "Poco/RefCountedObject.h"
namespace Poco {
namespace Crypto {
class CryptoTransform;
class Crypto_API Cipher: public Poco::RefCountedObject
/// Represents the abstract base class from which all implementations of
/// symmetric/asymmetric encryption algorithms must inherit. Use the CipherFactory
/// class to obtain an instance of this class:
///
/// CipherFactory& factory = CipherFactory::defaultFactory();
/// // Creates a 256-bit AES cipher
/// Cipher* pCipher = factory.createCipher(CipherKey("aes-256"));
/// Cipher* pRSACipher = factory.createCipher(RSAKey(RSAKey::KL_1024, RSAKey::EXP_SMALL));
///
/// Check the different Key constructors on how to initialize/create
/// a key. The above example auto-generates random keys.
///
/// Note that you won't be able to decrypt data encrypted with a random key
/// once the Cipher is destroyed unless you persist the generated key and IV.
/// An example usage for random keys is to encrypt data saved in a temporary
/// file.
///
/// Once your key is set up, you can use the Cipher object to encrypt or
/// decrypt strings or, in conjunction with a CryptoInputStream or a
/// CryptoOutputStream, to encrypt streams of data.
///
/// Since encrypted strings will contain arbitrary binary data that will cause
/// problems in applications that are not binary-safe (eg., when sending
/// encrypted data in e-mails), the encryptString() and decryptString() can
/// encode (or decode, respectively) encrypted data using a "transport encoding".
/// Supported encodings are Base64 and BinHex.
///
/// The following example encrypts and decrypts a string utilizing Base64
/// encoding:
///
/// std::string plainText = "This is my secret information";
/// std::string encrypted = pCipher->encryptString(plainText, Cipher::ENC_BASE64);
/// std::string decrypted = pCipher->decryptString(encrypted, Cipher::ENC_BASE64);
///
/// In order to encrypt a stream of data (eg. to encrypt files), you can use
/// a CryptoStream:
///
/// // Create an output stream that will encrypt all data going through it
/// // and write pass it to the underlying file stream.
/// Poco::FileOutputStream sink("encrypted.dat");
/// CryptoOutputStream encryptor(sink, pCipher->createEncryptor());
///
/// Poco::FileInputStream source("source.txt");
/// Poco::StreamCopier::copyStream(source, encryptor);
///
/// // Always close output streams to flush all internal buffers
/// encryptor.close();
/// sink.close();
namespace Poco
{
namespace Crypto
{
public:
typedef Poco::AutoPtr<Cipher> Ptr;
typedef std::vector<unsigned char> ByteVec;
enum Encoding
/// Transport encoding to use for encryptString() and decryptString().
{
ENC_NONE = 0x00, /// Plain binary output
ENC_BASE64 = 0x01, /// Base64-encoded output
ENC_BINHEX = 0x02, /// BinHex-encoded output
ENC_BASE64_NO_LF = 0x81, /// Base64-encoded output, no linefeeds
ENC_BINHEX_NO_LF = 0x82 /// BinHex-encoded output, no linefeeds
};
virtual ~Cipher();
/// Destroys the Cipher.
virtual const std::string& name() const = 0;
/// Returns the name of the Cipher.
virtual CryptoTransform* createEncryptor() = 0;
/// Creates an encryptor object to be used with a CryptoStream.
virtual CryptoTransform* createDecryptor() = 0;
/// Creates a decryptor object to be used with a CryptoStream.
virtual std::string encryptString(const std::string& str, Encoding encoding = ENC_NONE);
/// Directly encrypt a string and encode it using the given encoding.
virtual std::string decryptString(const std::string& str, Encoding encoding = ENC_NONE);
/// Directly decrypt a string that is encoded with the given encoding.
virtual void encrypt(std::istream& source, std::ostream& sink, Encoding encoding = ENC_NONE);
/// Directly encrypts an input stream and encodes it using the given encoding.
virtual void decrypt(std::istream& source, std::ostream& sink, Encoding encoding = ENC_NONE);
/// Directly decrypt an input stream that is encoded with the given encoding.
protected:
Cipher();
/// Creates a new Cipher object.
private:
Cipher(const Cipher&);
Cipher& operator = (const Cipher&);
};
} } // namespace Poco::Crypto
class CryptoTransform;
class Crypto_API Cipher : public Poco::RefCountedObject
/// Represents the abstract base class from which all implementations of
/// symmetric/asymmetric encryption algorithms must inherit. Use the CipherFactory
/// class to obtain an instance of this class:
///
/// CipherFactory& factory = CipherFactory::defaultFactory();
/// // Creates a 256-bit AES cipher
/// Cipher* pCipher = factory.createCipher(CipherKey("aes-256"));
/// Cipher* pRSACipher = factory.createCipher(RSAKey(RSAKey::KL_1024, RSAKey::EXP_SMALL));
///
/// Check the different Key constructors on how to initialize/create
/// a key. The above example auto-generates random keys.
///
/// Note that you won't be able to decrypt data encrypted with a random key
/// once the Cipher is destroyed unless you persist the generated key and IV.
/// An example usage for random keys is to encrypt data saved in a temporary
/// file.
///
/// Once your key is set up, you can use the Cipher object to encrypt or
/// decrypt strings or, in conjunction with a CryptoInputStream or a
/// CryptoOutputStream, to encrypt streams of data.
///
/// Since encrypted strings will contain arbitrary binary data that will cause
/// problems in applications that are not binary-safe (eg., when sending
/// encrypted data in e-mails), the encryptString() and decryptString() can
/// encode (or decode, respectively) encrypted data using a "transport encoding".
/// Supported encodings are Base64 and BinHex.
///
/// The following example encrypts and decrypts a string utilizing Base64
/// encoding:
///
/// std::string plainText = "This is my secret information";
/// std::string encrypted = pCipher->encryptString(plainText, Cipher::ENC_BASE64);
/// std::string decrypted = pCipher->decryptString(encrypted, Cipher::ENC_BASE64);
///
/// In order to encrypt a stream of data (eg. to encrypt files), you can use
/// a CryptoStream:
///
/// // Create an output stream that will encrypt all data going through it
/// // and write pass it to the underlying file stream.
/// Poco::FileOutputStream sink("encrypted.dat");
/// CryptoOutputStream encryptor(sink, pCipher->createEncryptor());
///
/// Poco::FileInputStream source("source.txt");
/// Poco::StreamCopier::copyStream(source, encryptor);
///
/// // Always close output streams to flush all internal buffers
/// encryptor.close();
/// sink.close();
{
public:
typedef Poco::AutoPtr<Cipher> Ptr;
typedef std::vector<unsigned char> ByteVec;
enum Encoding
/// Transport encoding to use for encryptString() and decryptString().
{
ENC_NONE = 0x00, /// Plain binary output
ENC_BASE64 = 0x01, /// Base64-encoded output
ENC_BINHEX = 0x02, /// BinHex-encoded output
ENC_BASE64_NO_LF = 0x81, /// Base64-encoded output, no linefeeds
ENC_BINHEX_NO_LF = 0x82 /// BinHex-encoded output, no linefeeds
};
virtual ~Cipher();
/// Destroys the Cipher.
virtual const std::string & name() const = 0;
/// Returns the name of the Cipher.
virtual CryptoTransform * createEncryptor() = 0;
/// Creates an encryptor object to be used with a CryptoStream.
virtual CryptoTransform * createDecryptor() = 0;
/// Creates a decryptor object to be used with a CryptoStream.
virtual std::string encryptString(const std::string & str, Encoding encoding = ENC_NONE);
/// Directly encrypt a string and encode it using the given encoding.
virtual std::string decryptString(const std::string & str, Encoding encoding = ENC_NONE);
/// Directly decrypt a string that is encoded with the given encoding.
virtual void encrypt(std::istream & source, std::ostream & sink, Encoding encoding = ENC_NONE);
/// Directly encrypts an input stream and encodes it using the given encoding.
virtual void decrypt(std::istream & source, std::ostream & sink, Encoding encoding = ENC_NONE);
/// Directly decrypt an input stream that is encoded with the given encoding.
protected:
Cipher();
/// Creates a new Cipher object.
private:
Cipher(const Cipher &);
Cipher & operator=(const Cipher &);
};
}
} // namespace Poco::Crypto
#endif // Crypto_Cipher_INCLUDED

View File

@ -21,55 +21,58 @@
#include "Poco/Crypto/Crypto.h"
namespace Poco {
namespace Crypto {
class Cipher;
class CipherKey;
class RSAKey;
class Crypto_API CipherFactory
/// A factory for Cipher objects. See the Cipher class for examples on how to
/// use the CipherFactory.
namespace Poco
{
namespace Crypto
{
public:
CipherFactory();
/// Creates a new CipherFactory object.
virtual ~CipherFactory();
/// Destroys the CipherFactory.
Cipher* createCipher(const CipherKey& key);
/// Creates a Cipher object for the given Cipher name. Valid cipher
/// names depend on the OpenSSL version the library is linked with;
/// see the output of
///
/// openssl enc --help
///
/// for a list of supported block and stream ciphers.
///
/// Common examples are:
///
/// * AES: "aes-128", "aes-256"
/// * DES: "des", "des3"
/// * Blowfish: "bf"
Cipher* createCipher(const RSAKey& key, RSAPaddingMode paddingMode = RSA_PADDING_PKCS1);
/// Creates a RSACipher using the given RSA key and padding mode
/// for public key encryption/private key decryption.
static CipherFactory& defaultFactory();
/// Returns the default CipherFactory.
private:
CipherFactory(const CipherFactory&);
CipherFactory& operator = (const CipherFactory&);
};
} } // namespace Poco::Crypto
class Cipher;
class CipherKey;
class RSAKey;
class Crypto_API CipherFactory
/// A factory for Cipher objects. See the Cipher class for examples on how to
/// use the CipherFactory.
{
public:
CipherFactory();
/// Creates a new CipherFactory object.
virtual ~CipherFactory();
/// Destroys the CipherFactory.
Cipher * createCipher(const CipherKey & key);
/// Creates a Cipher object for the given Cipher name. Valid cipher
/// names depend on the OpenSSL version the library is linked with;
/// see the output of
///
/// openssl enc --help
///
/// for a list of supported block and stream ciphers.
///
/// Common examples are:
///
/// * AES: "aes-128", "aes-256"
/// * DES: "des", "des3"
/// * Blowfish: "bf"
Cipher * createCipher(const RSAKey & key, RSAPaddingMode paddingMode = RSA_PADDING_PKCS1);
/// Creates a RSACipher using the given RSA key and padding mode
/// for public key encryption/private key decryption.
static CipherFactory & defaultFactory();
/// Returns the default CipherFactory.
private:
CipherFactory(const CipherFactory &);
CipherFactory & operator=(const CipherFactory &);
};
}
} // namespace Poco::Crypto
#endif // Crypto_CipherFactory_INCLUDED

View File

@ -18,52 +18,55 @@
#define Crypto_CipherImpl_INCLUDED
#include "Poco/Crypto/Crypto.h"
#include <openssl/evp.h>
#include "Poco/Crypto/Cipher.h"
#include "Poco/Crypto/CipherKey.h"
#include "Poco/Crypto/Crypto.h"
#include "Poco/Crypto/OpenSSLInitializer.h"
#include <openssl/evp.h>
namespace Poco {
namespace Crypto {
class CipherImpl: public Cipher
/// An implementation of the Cipher class for OpenSSL's crypto library.
namespace Poco
{
public:
CipherImpl(const CipherKey& key);
/// Creates a new CipherImpl object for the given CipherKey.
virtual ~CipherImpl();
/// Destroys the CipherImpl.
const std::string& name() const;
/// Returns the name of the cipher.
CryptoTransform* createEncryptor();
/// Creates an encryptor object.
CryptoTransform* createDecryptor();
/// Creates a decryptor object.
private:
CipherKey _key;
OpenSSLInitializer _openSSLInitializer;
};
//
// Inlines
//
inline const std::string& CipherImpl::name() const
namespace Crypto
{
return _key.name();
class CipherImpl : public Cipher
/// An implementation of the Cipher class for OpenSSL's crypto library.
{
public:
CipherImpl(const CipherKey & key);
/// Creates a new CipherImpl object for the given CipherKey.
virtual ~CipherImpl();
/// Destroys the CipherImpl.
const std::string & name() const;
/// Returns the name of the cipher.
CryptoTransform * createEncryptor();
/// Creates an encryptor object.
CryptoTransform * createDecryptor();
/// Creates a decryptor object.
private:
CipherKey _key;
OpenSSLInitializer _openSSLInitializer;
};
//
// Inlines
//
inline const std::string & CipherImpl::name() const
{
return _key.name();
}
}
} } // namespace Poco::Crypto
} // namespace Poco::Crypto
#endif // Crypto_CipherImpl_INCLUDED

View File

@ -18,184 +18,186 @@
#define Crypto_CipherKey_INCLUDED
#include "Poco/Crypto/Crypto.h"
#include "Poco/Crypto/CipherKeyImpl.h"
#include "Poco/Crypto/Crypto.h"
namespace Poco {
namespace Crypto {
class Crypto_API CipherKey
/// CipherKey stores the key information for decryption/encryption of data.
/// To create a random key, using the following code:
///
/// CipherKey key("aes-256");
///
/// Note that you won't be able to decrypt data encrypted with a random key
/// once the Cipher is destroyed unless you persist the generated key and IV.
/// An example usage for random keys is to encrypt data saved in a temporary
/// file.
///
/// To create a key using a human-readable password
/// string, use the following code. We create a AES Cipher and
/// use a salt value to make the key more robust:
///
/// std::string password = "secret";
/// std::string salt("asdff8723lasdf(**923412");
/// CipherKey key("aes-256", password, salt);
///
/// You may also control the digest and the number of iterations used to generate the key
/// by specifying the specific values. Here we create a key with the same data as before,
/// except that we use 100 iterations instead of DEFAULT_ITERATION_COUNT, and sha1 instead of
/// the default md5:
///
/// std::string password = "secret";
/// std::string salt("asdff8723lasdf(**923412");
/// std::string digest ("sha1");
/// CipherKey key("aes-256", password, salt, 100, digest);
///
namespace Poco
{
public:
typedef CipherKeyImpl::Mode Mode;
typedef CipherKeyImpl::ByteVec ByteVec;
enum
{
DEFAULT_ITERATION_COUNT = 2000
/// Default iteration count to use with
/// generateKey(). RSA security recommends
/// an iteration count of at least 1000.
};
CipherKey(const std::string& name,
const std::string& passphrase,
const std::string& salt = "",
int iterationCount = DEFAULT_ITERATION_COUNT,
const std::string& digest = "md5");
/// Creates a new CipherKeyImpl object using the given
/// cipher name, passphrase, salt value, iteration count and digest.
CipherKey(const std::string& name,
const ByteVec& key,
const ByteVec& iv);
/// Creates a new CipherKeyImpl object using the given cipher
/// name, key and initialization vector (IV).
///
/// The size of the IV must match the cipher's expected
/// IV size (see ivSize()), except for GCM mode, which allows
/// a custom IV size.
CipherKey(const std::string& name);
/// Creates a new CipherKeyImpl object. Autoinitializes key and
/// initialization vector.
~CipherKey();
/// Destroys the CipherKeyImpl.
const std::string& name() const;
/// Returns the name of the Cipher.
int keySize() const;
/// Returns the key size of the Cipher.
int blockSize() const;
/// Returns the block size of the Cipher.
int ivSize() const;
/// Returns the IV size of the Cipher.
Mode mode() const;
/// Returns the Cipher's mode of operation.
const ByteVec& getKey() const;
/// Returns the key for the Cipher.
void setKey(const ByteVec& key);
/// Sets the key for the Cipher.
const ByteVec& getIV() const;
/// Returns the initialization vector (IV) for the Cipher.
void setIV(const ByteVec& iv);
/// Sets the initialization vector (IV) for the Cipher.
///
/// The size of the vector must match the cipher's expected
/// IV size (see ivSize()), except for GCM mode, which allows
/// a custom IV size.
CipherKeyImpl::Ptr impl();
/// Returns the impl object
private:
CipherKeyImpl::Ptr _pImpl;
};
//
// inlines
//
inline const std::string& CipherKey::name() const
namespace Crypto
{
return _pImpl->name();
class Crypto_API CipherKey
/// CipherKey stores the key information for decryption/encryption of data.
/// To create a random key, using the following code:
///
/// CipherKey key("aes-256");
///
/// Note that you won't be able to decrypt data encrypted with a random key
/// once the Cipher is destroyed unless you persist the generated key and IV.
/// An example usage for random keys is to encrypt data saved in a temporary
/// file.
///
/// To create a key using a human-readable password
/// string, use the following code. We create a AES Cipher and
/// use a salt value to make the key more robust:
///
/// std::string password = "secret";
/// std::string salt("asdff8723lasdf(**923412");
/// CipherKey key("aes-256", password, salt);
///
/// You may also control the digest and the number of iterations used to generate the key
/// by specifying the specific values. Here we create a key with the same data as before,
/// except that we use 100 iterations instead of DEFAULT_ITERATION_COUNT, and sha1 instead of
/// the default md5:
///
/// std::string password = "secret";
/// std::string salt("asdff8723lasdf(**923412");
/// std::string digest ("sha1");
/// CipherKey key("aes-256", password, salt, 100, digest);
///
{
public:
typedef CipherKeyImpl::Mode Mode;
typedef CipherKeyImpl::ByteVec ByteVec;
enum
{
DEFAULT_ITERATION_COUNT = 2000
/// Default iteration count to use with
/// generateKey(). RSA security recommends
/// an iteration count of at least 1000.
};
CipherKey(
const std::string & name,
const std::string & passphrase,
const std::string & salt = "",
int iterationCount = DEFAULT_ITERATION_COUNT,
const std::string & digest = "md5");
/// Creates a new CipherKeyImpl object using the given
/// cipher name, passphrase, salt value, iteration count and digest.
CipherKey(const std::string & name, const ByteVec & key, const ByteVec & iv);
/// Creates a new CipherKeyImpl object using the given cipher
/// name, key and initialization vector (IV).
///
/// The size of the IV must match the cipher's expected
/// IV size (see ivSize()), except for GCM mode, which allows
/// a custom IV size.
CipherKey(const std::string & name);
/// Creates a new CipherKeyImpl object. Autoinitializes key and
/// initialization vector.
~CipherKey();
/// Destroys the CipherKeyImpl.
const std::string & name() const;
/// Returns the name of the Cipher.
int keySize() const;
/// Returns the key size of the Cipher.
int blockSize() const;
/// Returns the block size of the Cipher.
int ivSize() const;
/// Returns the IV size of the Cipher.
Mode mode() const;
/// Returns the Cipher's mode of operation.
const ByteVec & getKey() const;
/// Returns the key for the Cipher.
void setKey(const ByteVec & key);
/// Sets the key for the Cipher.
const ByteVec & getIV() const;
/// Returns the initialization vector (IV) for the Cipher.
void setIV(const ByteVec & iv);
/// Sets the initialization vector (IV) for the Cipher.
///
/// The size of the vector must match the cipher's expected
/// IV size (see ivSize()), except for GCM mode, which allows
/// a custom IV size.
CipherKeyImpl::Ptr impl();
/// Returns the impl object
private:
CipherKeyImpl::Ptr _pImpl;
};
//
// inlines
//
inline const std::string & CipherKey::name() const
{
return _pImpl->name();
}
inline int CipherKey::keySize() const
{
return _pImpl->keySize();
}
inline int CipherKey::blockSize() const
{
return _pImpl->blockSize();
}
inline int CipherKey::ivSize() const
{
return _pImpl->ivSize();
}
inline CipherKey::Mode CipherKey::mode() const
{
return _pImpl->mode();
}
inline const CipherKey::ByteVec & CipherKey::getKey() const
{
return _pImpl->getKey();
}
inline void CipherKey::setKey(const CipherKey::ByteVec & key)
{
_pImpl->setKey(key);
}
inline const CipherKey::ByteVec & CipherKey::getIV() const
{
return _pImpl->getIV();
}
inline void CipherKey::setIV(const CipherKey::ByteVec & iv)
{
_pImpl->setIV(iv);
}
inline CipherKeyImpl::Ptr CipherKey::impl()
{
return _pImpl;
}
}
inline int CipherKey::keySize() const
{
return _pImpl->keySize();
}
inline int CipherKey::blockSize() const
{
return _pImpl->blockSize();
}
inline int CipherKey::ivSize() const
{
return _pImpl->ivSize();
}
inline CipherKey::Mode CipherKey::mode() const
{
return _pImpl->mode();
}
inline const CipherKey::ByteVec& CipherKey::getKey() const
{
return _pImpl->getKey();
}
inline void CipherKey::setKey(const CipherKey::ByteVec& key)
{
_pImpl->setKey(key);
}
inline const CipherKey::ByteVec& CipherKey::getIV() const
{
return _pImpl->getIV();
}
inline void CipherKey::setIV(const CipherKey::ByteVec& iv)
{
_pImpl->setIV(iv);
}
inline CipherKeyImpl::Ptr CipherKey::impl()
{
return _pImpl;
}
} } // namespace Poco::Crypto
} // namespace Poco::Crypto
#endif // Crypto_CipherKey_INCLUDED

View File

@ -18,151 +18,151 @@
#define Crypto_CipherKeyImpl_INCLUDED
#include <vector>
#include "Poco/AutoPtr.h"
#include "Poco/Crypto/Crypto.h"
#include "Poco/Crypto/OpenSSLInitializer.h"
#include "Poco/RefCountedObject.h"
#include "Poco/AutoPtr.h"
#include <vector>
struct evp_cipher_st;
typedef struct evp_cipher_st EVP_CIPHER;
namespace Poco {
namespace Crypto {
class CipherKeyImpl: public RefCountedObject
/// An implementation of the CipherKey class for OpenSSL's crypto library.
namespace Poco
{
public:
typedef std::vector<unsigned char> ByteVec;
typedef Poco::AutoPtr<CipherKeyImpl> Ptr;
enum Mode
/// Cipher mode of operation. This mode determines how multiple blocks
/// are connected; this is essential to improve security.
{
MODE_STREAM_CIPHER, /// Stream cipher
MODE_ECB, /// Electronic codebook (plain concatenation)
MODE_CBC, /// Cipher block chaining (default)
MODE_CFB, /// Cipher feedback
MODE_OFB, /// Output feedback
MODE_CTR, /// Counter mode
MODE_GCM, /// Galois/Counter mode
MODE_CCM /// Counter with CBC-MAC
};
CipherKeyImpl(const std::string& name,
const std::string& passphrase,
const std::string& salt,
int iterationCount,
const std::string& digest);
/// Creates a new CipherKeyImpl object, using
/// the given cipher name, passphrase, salt value
/// and iteration count.
CipherKeyImpl(const std::string& name,
const ByteVec& key,
const ByteVec& iv);
/// Creates a new CipherKeyImpl object, using the
/// given cipher name, key and initialization vector.
CipherKeyImpl(const std::string& name);
/// Creates a new CipherKeyImpl object. Autoinitializes key
/// and initialization vector.
virtual ~CipherKeyImpl();
/// Destroys the CipherKeyImpl.
const std::string& name() const;
/// Returns the name of the Cipher.
int keySize() const;
/// Returns the key size of the Cipher.
int blockSize() const;
/// Returns the block size of the Cipher.
int ivSize() const;
/// Returns the IV size of the Cipher.
Mode mode() const;
/// Returns the Cipher's mode of operation.
const ByteVec& getKey() const;
/// Returns the key for the Cipher.
void setKey(const ByteVec& key);
/// Sets the key for the Cipher.
const ByteVec& getIV() const;
/// Returns the initialization vector (IV) for the Cipher.
void setIV(const ByteVec& iv);
/// Sets the initialization vector (IV) for the Cipher.
const EVP_CIPHER* cipher();
/// Returns the cipher object
private:
void generateKey(const std::string& passphrase,
const std::string& salt,
int iterationCount);
/// Generates key and IV from a password and optional salt string.
void generateKey();
/// Generates key and IV from random data.
void getRandomBytes(ByteVec& vec, std::size_t count);
/// Stores random bytes in vec.
private:
const EVP_CIPHER* _pCipher;
const EVP_MD* _pDigest;
std::string _name;
ByteVec _key;
ByteVec _iv;
OpenSSLInitializer _openSSLInitializer;
};
//
// Inlines
//
inline const std::string& CipherKeyImpl::name() const
namespace Crypto
{
return _name;
class CipherKeyImpl : public RefCountedObject
/// An implementation of the CipherKey class for OpenSSL's crypto library.
{
public:
typedef std::vector<unsigned char> ByteVec;
typedef Poco::AutoPtr<CipherKeyImpl> Ptr;
enum Mode
/// Cipher mode of operation. This mode determines how multiple blocks
/// are connected; this is essential to improve security.
{
MODE_STREAM_CIPHER, /// Stream cipher
MODE_ECB, /// Electronic codebook (plain concatenation)
MODE_CBC, /// Cipher block chaining (default)
MODE_CFB, /// Cipher feedback
MODE_OFB, /// Output feedback
MODE_CTR, /// Counter mode
MODE_GCM, /// Galois/Counter mode
MODE_CCM /// Counter with CBC-MAC
};
CipherKeyImpl(
const std::string & name,
const std::string & passphrase,
const std::string & salt,
int iterationCount,
const std::string & digest);
/// Creates a new CipherKeyImpl object, using
/// the given cipher name, passphrase, salt value
/// and iteration count.
CipherKeyImpl(const std::string & name, const ByteVec & key, const ByteVec & iv);
/// Creates a new CipherKeyImpl object, using the
/// given cipher name, key and initialization vector.
CipherKeyImpl(const std::string & name);
/// Creates a new CipherKeyImpl object. Autoinitializes key
/// and initialization vector.
virtual ~CipherKeyImpl();
/// Destroys the CipherKeyImpl.
const std::string & name() const;
/// Returns the name of the Cipher.
int keySize() const;
/// Returns the key size of the Cipher.
int blockSize() const;
/// Returns the block size of the Cipher.
int ivSize() const;
/// Returns the IV size of the Cipher.
Mode mode() const;
/// Returns the Cipher's mode of operation.
const ByteVec & getKey() const;
/// Returns the key for the Cipher.
void setKey(const ByteVec & key);
/// Sets the key for the Cipher.
const ByteVec & getIV() const;
/// Returns the initialization vector (IV) for the Cipher.
void setIV(const ByteVec & iv);
/// Sets the initialization vector (IV) for the Cipher.
const EVP_CIPHER * cipher();
/// Returns the cipher object
private:
void generateKey(const std::string & passphrase, const std::string & salt, int iterationCount);
/// Generates key and IV from a password and optional salt string.
void generateKey();
/// Generates key and IV from random data.
void getRandomBytes(ByteVec & vec, std::size_t count);
/// Stores random bytes in vec.
private:
const EVP_CIPHER * _pCipher;
const EVP_MD * _pDigest;
std::string _name;
ByteVec _key;
ByteVec _iv;
OpenSSLInitializer _openSSLInitializer;
};
//
// Inlines
//
inline const std::string & CipherKeyImpl::name() const
{
return _name;
}
inline const CipherKeyImpl::ByteVec & CipherKeyImpl::getKey() const
{
return _key;
}
inline void CipherKeyImpl::setKey(const ByteVec & key)
{
poco_assert(key.size() == static_cast<ByteVec::size_type>(keySize()));
_key = key;
}
inline const CipherKeyImpl::ByteVec & CipherKeyImpl::getIV() const
{
return _iv;
}
inline const EVP_CIPHER * CipherKeyImpl::cipher()
{
return _pCipher;
}
}
inline const CipherKeyImpl::ByteVec& CipherKeyImpl::getKey() const
{
return _key;
}
inline void CipherKeyImpl::setKey(const ByteVec& key)
{
poco_assert(key.size() == static_cast<ByteVec::size_type>(keySize()));
_key = key;
}
inline const CipherKeyImpl::ByteVec& CipherKeyImpl::getIV() const
{
return _iv;
}
inline const EVP_CIPHER* CipherKeyImpl::cipher()
{
return _pCipher;
}
} } // namespace Poco::Crypto
} // namespace Poco::Crypto
#endif // Crypto_CipherKeyImpl_INCLUDED

View File

@ -24,39 +24,37 @@
#define POCO_EXTERNAL_OPENSSL_SLPRO 2
#include "Poco/Foundation.h"
#include <openssl/opensslv.h>
#include "Poco/Foundation.h"
#ifndef OPENSSL_VERSION_PREREQ
#if defined(OPENSSL_VERSION_MAJOR) && defined(OPENSSL_VERSION_MINOR)
#define OPENSSL_VERSION_PREREQ(maj, min) \
((OPENSSL_VERSION_MAJOR << 16) + OPENSSL_VERSION_MINOR >= ((maj) << 16) + (min))
#else
#define OPENSSL_VERSION_PREREQ(maj, min) \
(OPENSSL_VERSION_NUMBER >= (((maj) << 28) | ((min) << 20)))
#endif
# if defined(OPENSSL_VERSION_MAJOR) && defined(OPENSSL_VERSION_MINOR)
# define OPENSSL_VERSION_PREREQ(maj, min) ((OPENSSL_VERSION_MAJOR << 16) + OPENSSL_VERSION_MINOR >= ((maj) << 16) + (min))
# else
# define OPENSSL_VERSION_PREREQ(maj, min) (OPENSSL_VERSION_NUMBER >= (((maj) << 28) | ((min) << 20)))
# endif
#endif
enum RSAPaddingMode
/// The padding mode used for RSA public key encryption.
/// The padding mode used for RSA public key encryption.
{
RSA_PADDING_PKCS1,
/// PKCS #1 v1.5 padding. This currently is the most widely used mode.
RSA_PADDING_PKCS1_OAEP,
/// EME-OAEP as defined in PKCS #1 v2.0 with SHA-1, MGF1 and an empty
/// encoding parameter. This mode is recommended for all new applications.
RSA_PADDING_SSLV23,
/// PKCS #1 v1.5 padding with an SSL-specific modification that denotes
/// that the server is SSL3 capable.
RSA_PADDING_NONE
/// Raw RSA encryption. This mode should only be used to implement cryptographically
/// sound padding modes in the application code. Encrypting user data directly with RSA
/// is insecure.
RSA_PADDING_PKCS1,
/// PKCS #1 v1.5 padding. This currently is the most widely used mode.
RSA_PADDING_PKCS1_OAEP,
/// EME-OAEP as defined in PKCS #1 v2.0 with SHA-1, MGF1 and an empty
/// encoding parameter. This mode is recommended for all new applications.
RSA_PADDING_SSLV23,
/// PKCS #1 v1.5 padding with an SSL-specific modification that denotes
/// that the server is SSL3 capable.
RSA_PADDING_NONE
/// Raw RSA encryption. This mode should only be used to implement cryptographically
/// sound padding modes in the application code. Encrypting user data directly with RSA
/// is insecure.
};
@ -68,128 +66,51 @@ enum RSAPaddingMode
// Crypto_API functions as being imported from a DLL, whereas this DLL sees symbols
// defined with this macro as being exported.
//
#if defined(_WIN32)
#if defined(POCO_DLL)
#if defined(Crypto_EXPORTS)
#define Crypto_API __declspec(dllexport)
#else
#define Crypto_API __declspec(dllimport)
#endif
#endif
#endif
#if !defined(Crypto_API)
#if !defined(POCO_NO_GCC_API_ATTRIBUTE) && defined (__GNUC__) && (__GNUC__ >= 4)
#define Crypto_API __attribute__ ((visibility ("default")))
#else
#define Crypto_API
#endif
# if !defined(POCO_NO_GCC_API_ATTRIBUTE) && defined(__GNUC__) && (__GNUC__ >= 4)
# define Crypto_API __attribute__((visibility("default")))
# else
# define Crypto_API
# endif
#endif
//
// Automatically link Crypto and OpenSSL libraries.
//
#if defined(_MSC_VER)
#if !defined(POCO_NO_AUTOMATIC_LIBS)
#if defined(POCO_INTERNAL_OPENSSL_MSVC_VER)
#if defined(POCO_EXTERNAL_OPENSSL)
#pragma message("External OpenSSL defined but internal headers used - possible mismatch!")
#endif // POCO_EXTERNAL_OPENSSL
#if !defined(_DEBUG)
#define POCO_DEBUG_SUFFIX ""
#if !defined (_DLL)
#define POCO_STATIC_SUFFIX "mt"
#else // _DLL
#define POCO_STATIC_SUFFIX ""
#endif
#else // _DEBUG
#define POCO_DEBUG_SUFFIX "d"
#if !defined (_DLL)
#define POCO_STATIC_SUFFIX "mt"
#else // _DLL
#define POCO_STATIC_SUFFIX ""
#endif
#endif
#pragma comment(lib, "libcrypto" POCO_STATIC_SUFFIX POCO_DEBUG_SUFFIX ".lib")
#pragma comment(lib, "libssl" POCO_STATIC_SUFFIX POCO_DEBUG_SUFFIX ".lib")
#if !defined(_WIN64) && !defined (_DLL) && \
(POCO_INTERNAL_OPENSSL_MSVC_VER == 120) && \
(POCO_MSVC_VERSION < POCO_INTERNAL_OPENSSL_MSVC_VER)
#pragma comment(lib, "libPreVS2013CRT" POCO_STATIC_SUFFIX POCO_DEBUG_SUFFIX ".lib")
#endif
#if !defined (_DLL) && (POCO_MSVS_VERSION >= 2015)
#pragma comment(lib, "legacy_stdio_definitions.lib")
#pragma comment(lib, "legacy_stdio_wide_specifiers.lib")
#endif
#elif defined(POCO_EXTERNAL_OPENSSL)
#if POCO_EXTERNAL_OPENSSL == POCO_EXTERNAL_OPENSSL_SLPRO
#if defined(POCO_DLL)
#if OPENSSL_VERSION_PREREQ(1,1)
#pragma comment(lib, "libcrypto.lib")
#pragma comment(lib, "libssl.lib")
#else
#pragma comment(lib, "libeay32.lib")
#pragma comment(lib, "ssleay32.lib")
#endif
#else
#if OPENSSL_VERSION_PREREQ(1,1)
#if defined(_WIN64)
#pragma comment(lib, "libcrypto64" POCO_LIB_SUFFIX)
#pragma comment(lib, "libssl64" POCO_LIB_SUFFIX)
#else
#pragma comment(lib, "libcrypto32" POCO_LIB_SUFFIX)
#pragma comment(lib, "libssl32" POCO_LIB_SUFFIX)
#endif
#else
#pragma comment(lib, "libeay32" POCO_LIB_SUFFIX)
#pragma comment(lib, "ssleay32" POCO_LIB_SUFFIX)
#endif
#endif
#elif POCO_EXTERNAL_OPENSSL == POCO_EXTERNAL_OPENSSL_DEFAULT
#if OPENSSL_VERSION_PREREQ(1,1)
#pragma comment(lib, "libcrypto.lib")
#pragma comment(lib, "libssl.lib")
#else
#pragma comment(lib, "libeay32.lib")
#pragma comment(lib, "ssleay32.lib")
#endif
#endif
#endif // POCO_INTERNAL_OPENSSL_MSVC_VER
#if !defined(Crypto_EXPORTS)
#pragma comment(lib, "PocoCrypto" POCO_LIB_SUFFIX)
#endif
#endif // POCO_NO_AUTOMATIC_LIBS
#endif
namespace Poco {
namespace Crypto {
namespace Poco
{
namespace Crypto
{
void Crypto_API initializeCrypto();
/// Initialize the Crypto library, as well as the underlying OpenSSL
/// libraries, by calling OpenSSLInitializer::initialize().
///
/// Should be called before using any class from the Crypto library.
/// The Crypto library will be initialized automatically, through
/// OpenSSLInitializer instances held by various Crypto classes
/// (Cipher, CipherKey, RSAKey, X509Certificate).
/// However, it is recommended to call initializeCrypto()
/// in any case at application startup.
///
/// Can be called multiple times; however, for every call to
/// initializeCrypto(), a matching call to uninitializeCrypto()
/// must be performed.
void Crypto_API initializeCrypto();
/// Initialize the Crypto library, as well as the underlying OpenSSL
/// libraries, by calling OpenSSLInitializer::initialize().
///
/// Should be called before using any class from the Crypto library.
/// The Crypto library will be initialized automatically, through
/// OpenSSLInitializer instances held by various Crypto classes
/// (Cipher, CipherKey, RSAKey, X509Certificate).
/// However, it is recommended to call initializeCrypto()
/// in any case at application startup.
///
/// Can be called multiple times; however, for every call to
/// initializeCrypto(), a matching call to uninitializeCrypto()
/// must be performed.
void Crypto_API uninitializeCrypto();
/// Uninitializes the Crypto library by calling
/// OpenSSLInitializer::uninitialize().
void Crypto_API uninitializeCrypto();
/// Uninitializes the Crypto library by calling
/// OpenSSLInitializer::uninitialize().
} } // namespace Poco::Crypto
}
} // namespace Poco::Crypto
#endif // Crypto_Crypto_INCLUDED

View File

@ -23,34 +23,37 @@
#include "Poco/Exception.h"
namespace Poco {
namespace Crypto {
POCO_DECLARE_EXCEPTION(Crypto_API, CryptoException, Poco::Exception)
class Crypto_API OpenSSLException : public CryptoException
namespace Poco
{
namespace Crypto
{
public:
OpenSSLException(int code = 0);
OpenSSLException(const std::string& msg, int code = 0);
OpenSSLException(const std::string& msg, const std::string& arg, int code = 0);
OpenSSLException(const std::string& msg, const Poco::Exception& exc, int code = 0);
OpenSSLException(const OpenSSLException& exc);
~OpenSSLException() throw();
OpenSSLException& operator = (const OpenSSLException& exc);
const char* name() const throw();
const char* className() const throw();
Poco::Exception* clone() const;
void rethrow() const;
private:
void setExtMessage();
};
} } // namespace Poco::Crypto
POCO_DECLARE_EXCEPTION(Crypto_API, CryptoException, Poco::Exception)
class Crypto_API OpenSSLException : public CryptoException
{
public:
OpenSSLException(int code = 0);
OpenSSLException(const std::string & msg, int code = 0);
OpenSSLException(const std::string & msg, const std::string & arg, int code = 0);
OpenSSLException(const std::string & msg, const Poco::Exception & exc, int code = 0);
OpenSSLException(const OpenSSLException & exc);
~OpenSSLException() throw();
OpenSSLException & operator=(const OpenSSLException & exc);
const char * name() const throw();
const char * className() const throw();
Poco::Exception * clone() const;
void rethrow() const;
private:
void setExtMessage();
};
}
} // namespace Poco::Crypto
#endif // Crypto_CryptoException_INCLUDED

View File

@ -19,174 +19,177 @@
#define Crypto_CryptoStream_INCLUDED
#include "Poco/Crypto/Crypto.h"
#include "Poco/BufferedStreamBuf.h"
#include "Poco/Buffer.h"
#include <iostream>
#include "Poco/Buffer.h"
#include "Poco/BufferedStreamBuf.h"
#include "Poco/Crypto/Crypto.h"
namespace Poco {
namespace Crypto {
class CryptoTransform;
class Cipher;
class Crypto_API CryptoStreamBuf: public Poco::BufferedStreamBuf
/// This stream buffer performs cryptographic transformation on the data
/// going through it.
namespace Poco
{
public:
CryptoStreamBuf(std::istream& istr, CryptoTransform* pTransform, std::streamsize bufferSize = 8192);
CryptoStreamBuf(std::ostream& ostr, CryptoTransform* pTransform, std::streamsize bufferSize = 8192);
virtual ~CryptoStreamBuf();
void close();
/// Flushes all buffers and finishes the encryption.
protected:
int readFromDevice(char* buffer, std::streamsize length);
int writeToDevice(const char* buffer, std::streamsize length);
private:
CryptoTransform* _pTransform;
std::istream* _pIstr;
std::ostream* _pOstr;
bool _eof;
Poco::Buffer<unsigned char> _buffer;
CryptoStreamBuf(const CryptoStreamBuf&);
CryptoStreamBuf& operator = (const CryptoStreamBuf&);
};
class Crypto_API CryptoIOS: public virtual std::ios
/// The base class for CryptoInputStream and CryptoOutputStream.
///
/// This class is needed to ensure correct initialization order of the
/// stream buffer and base classes.
namespace Crypto
{
public:
CryptoIOS(std::istream& istr, CryptoTransform* pTransform, std::streamsize bufferSize = 8192);
CryptoIOS(std::ostream& ostr, CryptoTransform* pTransform, std::streamsize bufferSize = 8192);
~CryptoIOS();
CryptoStreamBuf* rdbuf();
protected:
CryptoStreamBuf _buf;
};
class Crypto_API CryptoInputStream: public CryptoIOS, public std::istream
/// This stream transforms all data passing through it using the given
/// CryptoTransform.
///
/// Use a CryptoTransform object provided by Cipher::createEncrytor() or
/// Cipher::createDecryptor() to create an encrypting or decrypting stream,
/// respectively.
{
public:
CryptoInputStream(std::istream& istr, CryptoTransform* pTransform, std::streamsize bufferSize = 8192);
/// Create a new CryptoInputStream object. The CryptoInputStream takes the
/// ownership of the given CryptoTransform object.
CryptoInputStream(std::istream& istr, Cipher& cipher, std::streamsize bufferSize = 8192);
/// Create a new encrypting CryptoInputStream object using the given cipher.
~CryptoInputStream();
/// Destroys the CryptoInputStream.
};
class CryptoTransform;
class Cipher;
class Crypto_API CryptoOutputStream: public CryptoIOS, public std::ostream
/// This stream transforms all data passing through it using the given
/// CryptoTransform.
///
/// Use a CryptoTransform object provided by Cipher::createEncrytor() or
/// Cipher::createDecryptor() to create an encrypting or decrypting stream,
/// respectively.
///
/// After all data has been passed through the stream, close() must be called
/// to ensure completion of cryptographic transformation.
{
public:
CryptoOutputStream(std::ostream& ostr, CryptoTransform* pTransform, std::streamsize bufferSize = 8192);
/// Create a new CryptoOutputStream object. The CryptoOutputStream takes the
/// ownership of the given CryptoTransform object.
class Crypto_API CryptoStreamBuf : public Poco::BufferedStreamBuf
/// This stream buffer performs cryptographic transformation on the data
/// going through it.
{
public:
CryptoStreamBuf(std::istream & istr, CryptoTransform * pTransform, std::streamsize bufferSize = 8192);
CryptoStreamBuf(std::ostream & ostr, CryptoTransform * pTransform, std::streamsize bufferSize = 8192);
CryptoOutputStream(std::ostream& ostr, Cipher& cipher, std::streamsize bufferSize = 8192);
/// Create a new decrypting CryptoOutputStream object using the given cipher.
virtual ~CryptoStreamBuf();
~CryptoOutputStream();
/// Destroys the CryptoOutputStream.
void close();
/// Flushes all buffers and finishes the encryption.
void close();
/// Flushes all buffers and finishes the encryption.
};
protected:
int readFromDevice(char * buffer, std::streamsize length);
int writeToDevice(const char * buffer, std::streamsize length);
private:
CryptoTransform * _pTransform;
std::istream * _pIstr;
std::ostream * _pOstr;
bool _eof;
Poco::Buffer<unsigned char> _buffer;
CryptoStreamBuf(const CryptoStreamBuf &);
CryptoStreamBuf & operator=(const CryptoStreamBuf &);
};
class Crypto_API DecryptingInputStream: public CryptoIOS, public std::istream
/// This stream decrypts all data passing through it using the given
/// Cipher.
{
public:
DecryptingInputStream(std::istream& istr, Cipher& cipher, std::streamsize bufferSize = 8192);
/// Create a new DecryptingInputStream object using the given cipher.
class Crypto_API CryptoIOS : public virtual std::ios
/// The base class for CryptoInputStream and CryptoOutputStream.
///
/// This class is needed to ensure correct initialization order of the
/// stream buffer and base classes.
{
public:
CryptoIOS(std::istream & istr, CryptoTransform * pTransform, std::streamsize bufferSize = 8192);
CryptoIOS(std::ostream & ostr, CryptoTransform * pTransform, std::streamsize bufferSize = 8192);
~CryptoIOS();
CryptoStreamBuf * rdbuf();
~DecryptingInputStream();
/// Destroys the DecryptingInputStream.
};
protected:
CryptoStreamBuf _buf;
};
class Crypto_API DecryptingOutputStream: public CryptoIOS, public std::ostream
/// This stream decrypts all data passing through it using the given
/// Cipher.
{
public:
DecryptingOutputStream(std::ostream& ostr, Cipher& cipher, std::streamsize bufferSize = 8192);
/// Create a new DecryptingOutputStream object using the given cipher.
class Crypto_API CryptoInputStream : public CryptoIOS, public std::istream
/// This stream transforms all data passing through it using the given
/// CryptoTransform.
///
/// Use a CryptoTransform object provided by Cipher::createEncrytor() or
/// Cipher::createDecryptor() to create an encrypting or decrypting stream,
/// respectively.
{
public:
CryptoInputStream(std::istream & istr, CryptoTransform * pTransform, std::streamsize bufferSize = 8192);
/// Create a new CryptoInputStream object. The CryptoInputStream takes the
/// ownership of the given CryptoTransform object.
~DecryptingOutputStream();
/// Destroys the DecryptingOutputStream.
CryptoInputStream(std::istream & istr, Cipher & cipher, std::streamsize bufferSize = 8192);
/// Create a new encrypting CryptoInputStream object using the given cipher.
void close();
/// Flushes all buffers and finishes the decryption.
};
~CryptoInputStream();
/// Destroys the CryptoInputStream.
};
class Crypto_API EncryptingInputStream: public CryptoIOS, public std::istream
/// This stream encrypts all data passing through it using the given
/// Cipher.
{
public:
EncryptingInputStream(std::istream& istr, Cipher& cipher, std::streamsize bufferSize = 8192);
/// Create a new EncryptingInputStream object using the given cipher.
class Crypto_API CryptoOutputStream : public CryptoIOS, public std::ostream
/// This stream transforms all data passing through it using the given
/// CryptoTransform.
///
/// Use a CryptoTransform object provided by Cipher::createEncrytor() or
/// Cipher::createDecryptor() to create an encrypting or decrypting stream,
/// respectively.
///
/// After all data has been passed through the stream, close() must be called
/// to ensure completion of cryptographic transformation.
{
public:
CryptoOutputStream(std::ostream & ostr, CryptoTransform * pTransform, std::streamsize bufferSize = 8192);
/// Create a new CryptoOutputStream object. The CryptoOutputStream takes the
/// ownership of the given CryptoTransform object.
~EncryptingInputStream();
/// Destroys the EncryptingInputStream.
};
CryptoOutputStream(std::ostream & ostr, Cipher & cipher, std::streamsize bufferSize = 8192);
/// Create a new decrypting CryptoOutputStream object using the given cipher.
~CryptoOutputStream();
/// Destroys the CryptoOutputStream.
void close();
/// Flushes all buffers and finishes the encryption.
};
class Crypto_API EncryptingOutputStream: public CryptoIOS, public std::ostream
/// This stream encrypts all data passing through it using the given
/// Cipher.
{
public:
EncryptingOutputStream(std::ostream& ostr, Cipher& cipher, std::streamsize bufferSize = 8192);
/// Create a new EncryptingOutputStream object using the given cipher.
class Crypto_API DecryptingInputStream : public CryptoIOS, public std::istream
/// This stream decrypts all data passing through it using the given
/// Cipher.
{
public:
DecryptingInputStream(std::istream & istr, Cipher & cipher, std::streamsize bufferSize = 8192);
/// Create a new DecryptingInputStream object using the given cipher.
~EncryptingOutputStream();
/// Destroys the EncryptingOutputStream.
void close();
/// Flushes all buffers and finishes the encryption.
};
~DecryptingInputStream();
/// Destroys the DecryptingInputStream.
};
} } // namespace Poco::Crypto
class Crypto_API DecryptingOutputStream : public CryptoIOS, public std::ostream
/// This stream decrypts all data passing through it using the given
/// Cipher.
{
public:
DecryptingOutputStream(std::ostream & ostr, Cipher & cipher, std::streamsize bufferSize = 8192);
/// Create a new DecryptingOutputStream object using the given cipher.
~DecryptingOutputStream();
/// Destroys the DecryptingOutputStream.
void close();
/// Flushes all buffers and finishes the decryption.
};
class Crypto_API EncryptingInputStream : public CryptoIOS, public std::istream
/// This stream encrypts all data passing through it using the given
/// Cipher.
{
public:
EncryptingInputStream(std::istream & istr, Cipher & cipher, std::streamsize bufferSize = 8192);
/// Create a new EncryptingInputStream object using the given cipher.
~EncryptingInputStream();
/// Destroys the EncryptingInputStream.
};
class Crypto_API EncryptingOutputStream : public CryptoIOS, public std::ostream
/// This stream encrypts all data passing through it using the given
/// Cipher.
{
public:
EncryptingOutputStream(std::ostream & ostr, Cipher & cipher, std::streamsize bufferSize = 8192);
/// Create a new EncryptingOutputStream object using the given cipher.
~EncryptingOutputStream();
/// Destroys the EncryptingOutputStream.
void close();
/// Flushes all buffers and finishes the encryption.
};
}
} // namespace Poco::Crypto
#endif // Crypto_CryptoStream_INCLUDED

View File

@ -18,70 +18,71 @@
#define Crypto_CryptoTransform_INCLUDED
#include "Poco/Crypto/Crypto.h"
#include <ios>
#include "Poco/Crypto/Crypto.h"
namespace Poco {
namespace Crypto {
class Crypto_API CryptoTransform
/// This interface represents the basic operations for cryptographic
/// transformations to be used with a CryptoInputStream or a
/// CryptoOutputStream.
///
/// Implementations of this class are returned by the Cipher class to
/// perform encryption or decryption of data.
namespace Poco
{
namespace Crypto
{
public:
CryptoTransform();
/// Creates a new CryptoTransform object.
virtual ~CryptoTransform();
/// Destroys the CryptoTransform.
virtual std::size_t blockSize() const = 0;
/// Returns the block size for this CryptoTransform.
virtual int setPadding(int padding);
/// Enables or disables padding. By default encryption operations are padded using standard block
/// padding and the padding is checked and removed when decrypting. If the padding parameter is zero then
/// no padding is performed, the total amount of data encrypted or decrypted must then be a multiple of
/// the block size or an error will occur.
virtual std::string getTag(std::size_t tagSize = 16) = 0;
/// Returns the GCM tag after encrypting using GCM mode.
///
/// Must be called after finalize().
virtual void setTag(const std::string& tag) = 0;
/// Sets the GCM tag for authenticated decryption using GCM mode.
///
/// Must be set before finalize() is called, otherwise
/// decryption will fail.
virtual std::streamsize transform(
const unsigned char* input,
std::streamsize inputLength,
unsigned char* output,
std::streamsize outputLength) = 0;
/// Transforms a chunk of data. The inputLength is arbitrary and does not
/// need to be a multiple of the block size. The output buffer has a maximum
/// capacity of the given outputLength that must be at least
/// inputLength + blockSize() - 1
/// Returns the number of bytes written to the output buffer.
virtual std::streamsize finalize(unsigned char* output, std::streamsize length) = 0;
/// Finalizes the transformation. The output buffer must contain enough
/// space for at least two blocks, ie.
/// length >= 2*blockSize()
/// must be true. Returns the number of bytes written to the output
/// buffer.
};
} } // namespace Poco::Crypto
class Crypto_API CryptoTransform
/// This interface represents the basic operations for cryptographic
/// transformations to be used with a CryptoInputStream or a
/// CryptoOutputStream.
///
/// Implementations of this class are returned by the Cipher class to
/// perform encryption or decryption of data.
{
public:
CryptoTransform();
/// Creates a new CryptoTransform object.
virtual ~CryptoTransform();
/// Destroys the CryptoTransform.
virtual std::size_t blockSize() const = 0;
/// Returns the block size for this CryptoTransform.
virtual int setPadding(int padding);
/// Enables or disables padding. By default encryption operations are padded using standard block
/// padding and the padding is checked and removed when decrypting. If the padding parameter is zero then
/// no padding is performed, the total amount of data encrypted or decrypted must then be a multiple of
/// the block size or an error will occur.
virtual std::string getTag(std::size_t tagSize = 16) = 0;
/// Returns the GCM tag after encrypting using GCM mode.
///
/// Must be called after finalize().
virtual void setTag(const std::string & tag) = 0;
/// Sets the GCM tag for authenticated decryption using GCM mode.
///
/// Must be set before finalize() is called, otherwise
/// decryption will fail.
virtual std::streamsize
transform(const unsigned char * input, std::streamsize inputLength, unsigned char * output, std::streamsize outputLength)
= 0;
/// Transforms a chunk of data. The inputLength is arbitrary and does not
/// need to be a multiple of the block size. The output buffer has a maximum
/// capacity of the given outputLength that must be at least
/// inputLength + blockSize() - 1
/// Returns the number of bytes written to the output buffer.
virtual std::streamsize finalize(unsigned char * output, std::streamsize length) = 0;
/// Finalizes the transformation. The output buffer must contain enough
/// space for at least two blocks, ie.
/// length >= 2*blockSize()
/// must be true. Returns the number of bytes written to the output
/// buffer.
};
}
} // namespace Poco::Crypto
#endif // Crypto_CryptoTransform_INCLUDED

View File

@ -18,63 +18,66 @@
#define Crypto_DigestEngine_INCLUDED
#include <openssl/evp.h>
#include "Poco/Crypto/Crypto.h"
#include "Poco/Crypto/OpenSSLInitializer.h"
#include "Poco/DigestEngine.h"
#include <openssl/evp.h>
namespace Poco {
namespace Crypto {
class Crypto_API DigestEngine: public Poco::DigestEngine
/// This class implements a Poco::DigestEngine for all
/// digest algorithms supported by OpenSSL.
namespace Poco
{
public:
DigestEngine(const std::string& name);
/// Creates a DigestEngine using the digest with the given name
/// (e.g., "MD5", "SHA1", "SHA256", "SHA512", etc.).
/// See the OpenSSL documentation for a list of supported digest algorithms.
///
/// Throws a Poco::NotFoundException if no algorithm with the given name exists.
~DigestEngine();
/// Destroys the DigestEngine.
const std::string& algorithm() const;
/// Returns the name of the digest algorithm.
int nid() const;
/// Returns the NID (OpenSSL object identifier) of the digest algorithm.
// DigestEngine
std::size_t digestLength() const;
void reset();
const Poco::DigestEngine::Digest& digest();
protected:
void updateImpl(const void* data, std::size_t length);
private:
std::string _name;
EVP_MD_CTX* _pContext;
Poco::DigestEngine::Digest _digest;
OpenSSLInitializer _openSSLInitializer;
};
//
// inlines
//
inline const std::string& DigestEngine::algorithm() const
namespace Crypto
{
return _name;
class Crypto_API DigestEngine : public Poco::DigestEngine
/// This class implements a Poco::DigestEngine for all
/// digest algorithms supported by OpenSSL.
{
public:
DigestEngine(const std::string & name);
/// Creates a DigestEngine using the digest with the given name
/// (e.g., "MD5", "SHA1", "SHA256", "SHA512", etc.).
/// See the OpenSSL documentation for a list of supported digest algorithms.
///
/// Throws a Poco::NotFoundException if no algorithm with the given name exists.
~DigestEngine();
/// Destroys the DigestEngine.
const std::string & algorithm() const;
/// Returns the name of the digest algorithm.
int nid() const;
/// Returns the NID (OpenSSL object identifier) of the digest algorithm.
// DigestEngine
std::size_t digestLength() const;
void reset();
const Poco::DigestEngine::Digest & digest();
protected:
void updateImpl(const void * data, std::size_t length);
private:
std::string _name;
EVP_MD_CTX * _pContext;
Poco::DigestEngine::Digest _digest;
OpenSSLInitializer _openSSLInitializer;
};
//
// inlines
//
inline const std::string & DigestEngine::algorithm() const
{
return _name;
}
}
} } // namespace Poco::Crypto
} // namespace Poco::Crypto
#endif // Crypto_DigestEngine_INCLUDED

View File

@ -19,83 +19,85 @@
#define Crypto_ECDSADigestEngine_INCLUDED
#include "Poco/Crypto/Crypto.h"
#include "Poco/Crypto/ECKey.h"
#include "Poco/DigestEngine.h"
#include "Poco/Crypto/DigestEngine.h"
#include <istream>
#include <ostream>
#include "Poco/Crypto/Crypto.h"
#include "Poco/Crypto/DigestEngine.h"
#include "Poco/Crypto/ECKey.h"
#include "Poco/DigestEngine.h"
namespace Poco {
namespace Crypto {
class Crypto_API ECDSADigestEngine: public Poco::DigestEngine
/// This class implements a Poco::DigestEngine that can be
/// used to compute a secure digital signature.
///
/// First another Poco::Crypto::DigestEngine is created and
/// used to compute a cryptographic hash of the data to be
/// signed. Then, the hash value is encrypted, using
/// the ECDSA private key.
///
/// To verify a signature, pass it to the verify()
/// member function. It will decrypt the signature
/// using the ECDSA public key and compare the resulting
/// hash with the actual hash of the data.
namespace Poco
{
namespace Crypto
{
public:
ECDSADigestEngine(const ECKey& key, const std::string &name);
/// Creates the ECDSADigestEngine with the given ECDSA key,
/// using the hash algorithm with the given name
/// (e.g., "SHA1", "SHA256", "SHA512", etc.).
/// See the OpenSSL documentation for a list of supported digest algorithms.
///
/// Throws a Poco::NotFoundException if no algorithm with the given name exists.
~ECDSADigestEngine();
/// Destroys the ECDSADigestEngine.
std::size_t digestLength() const;
/// Returns the length of the digest in bytes.
void reset();
/// Resets the engine so that a new
/// digest can be computed.
const DigestEngine::Digest& digest();
/// Finishes the computation of the digest
/// (the first time it's called) and
/// returns the message digest.
///
/// Can be called multiple times.
const DigestEngine::Digest& signature();
/// Signs the digest using the ECDSADSA algorithm
/// and the private key (the first time it's
/// called) and returns the result.
///
/// Can be called multiple times.
bool verify(const DigestEngine::Digest& signature);
/// Verifies the data against the signature.
///
/// Returns true if the signature can be verified, false otherwise.
protected:
void updateImpl(const void* data, std::size_t length);
private:
ECKey _key;
Poco::Crypto::DigestEngine _engine;
Poco::DigestEngine::Digest _digest;
Poco::DigestEngine::Digest _signature;
};
} } // namespace Poco::Crypto
class Crypto_API ECDSADigestEngine : public Poco::DigestEngine
/// This class implements a Poco::DigestEngine that can be
/// used to compute a secure digital signature.
///
/// First another Poco::Crypto::DigestEngine is created and
/// used to compute a cryptographic hash of the data to be
/// signed. Then, the hash value is encrypted, using
/// the ECDSA private key.
///
/// To verify a signature, pass it to the verify()
/// member function. It will decrypt the signature
/// using the ECDSA public key and compare the resulting
/// hash with the actual hash of the data.
{
public:
ECDSADigestEngine(const ECKey & key, const std::string & name);
/// Creates the ECDSADigestEngine with the given ECDSA key,
/// using the hash algorithm with the given name
/// (e.g., "SHA1", "SHA256", "SHA512", etc.).
/// See the OpenSSL documentation for a list of supported digest algorithms.
///
/// Throws a Poco::NotFoundException if no algorithm with the given name exists.
~ECDSADigestEngine();
/// Destroys the ECDSADigestEngine.
std::size_t digestLength() const;
/// Returns the length of the digest in bytes.
void reset();
/// Resets the engine so that a new
/// digest can be computed.
const DigestEngine::Digest & digest();
/// Finishes the computation of the digest
/// (the first time it's called) and
/// returns the message digest.
///
/// Can be called multiple times.
const DigestEngine::Digest & signature();
/// Signs the digest using the ECDSADSA algorithm
/// and the private key (the first time it's
/// called) and returns the result.
///
/// Can be called multiple times.
bool verify(const DigestEngine::Digest & signature);
/// Verifies the data against the signature.
///
/// Returns true if the signature can be verified, false otherwise.
protected:
void updateImpl(const void * data, std::size_t length);
private:
ECKey _key;
Poco::Crypto::DigestEngine _engine;
Poco::DigestEngine::Digest _digest;
Poco::DigestEngine::Digest _signature;
};
}
} // namespace Poco::Crypto
#endif // Crypto_ECDSADigestEngine_INCLUDED

View File

@ -20,116 +20,119 @@
#include "Poco/Crypto/Crypto.h"
#include "Poco/Crypto/KeyPair.h"
#include "Poco/Crypto/ECKeyImpl.h"
#include "Poco/Crypto/KeyPair.h"
namespace Poco {
namespace Crypto {
class X509Certificate;
class PKCS12Container;
class Crypto_API ECKey : public KeyPair
/// This class stores an EC key pair, consisting
/// of private and public key. Storage of the private
/// key is optional.
///
/// If a private key is available, the ECKey can be
/// used for decrypting data (encrypted with the public key)
/// or computing secure digital signatures.
namespace Poco
{
public:
ECKey(const EVPPKey& key);
/// Constructs ECKeyImpl by extracting the EC key.
ECKey(const X509Certificate& cert);
/// Extracts the EC public key from the given certificate.
ECKey(const PKCS12Container& cert);
/// Extracts the EC private key from the given certificate.
ECKey(const std::string& eccGroup);
/// Creates the ECKey. Creates a new public/private key pair using the given parameters.
/// Can be used to sign data and verify signatures.
ECKey(const std::string& publicKeyFile, const std::string& privateKeyFile, const std::string& privateKeyPassphrase = "");
/// Creates the ECKey, by reading public and private key from the given files and
/// using the given passphrase for the private key.
///
/// Cannot be used for signing or decryption unless a private key is available.
///
/// If a private key is specified, you don't need to specify a public key file.
/// OpenSSL will auto-create the public key from the private key.
ECKey(std::istream* pPublicKeyStream, std::istream* pPrivateKeyStream = 0, const std::string& privateKeyPassphrase = "");
/// Creates the ECKey, by reading public and private key from the given streams and
/// using the given passphrase for the private key.
///
/// Cannot be used for signing or decryption unless a private key is available.
///
/// If a private key is specified, you don't need to specify a public key file.
/// OpenSSL will auto-create the public key from the private key.
~ECKey();
/// Destroys the ECKey.
ECKeyImpl::Ptr impl() const;
/// Returns the impl object.
static std::string getCurveName(int nid = -1);
/// Returns elliptical curve name corresponding to
/// the given nid; if nid is not found, returns
/// empty string.
///
/// If nid is -1, returns first curve name.
///
/// If no curves are found, returns empty string;
static int getCurveNID(std::string& name);
/// Returns the NID of the specified curve.
///
/// If name is empty, returns the first curve NID
/// and updates the name accordingly.
static bool hasCurve(const std::string& name);
/// Returns true if the named curve is found,
/// false otherwise.
private:
ECKeyImpl::Ptr _pImpl;
};
//
// inlines
//
inline ECKeyImpl::Ptr ECKey::impl() const
namespace Crypto
{
return _pImpl;
class X509Certificate;
class PKCS12Container;
class Crypto_API ECKey : public KeyPair
/// This class stores an EC key pair, consisting
/// of private and public key. Storage of the private
/// key is optional.
///
/// If a private key is available, the ECKey can be
/// used for decrypting data (encrypted with the public key)
/// or computing secure digital signatures.
{
public:
ECKey(const EVPPKey & key);
/// Constructs ECKeyImpl by extracting the EC key.
ECKey(const X509Certificate & cert);
/// Extracts the EC public key from the given certificate.
ECKey(const PKCS12Container & cert);
/// Extracts the EC private key from the given certificate.
ECKey(const std::string & eccGroup);
/// Creates the ECKey. Creates a new public/private key pair using the given parameters.
/// Can be used to sign data and verify signatures.
ECKey(const std::string & publicKeyFile, const std::string & privateKeyFile, const std::string & privateKeyPassphrase = "");
/// Creates the ECKey, by reading public and private key from the given files and
/// using the given passphrase for the private key.
///
/// Cannot be used for signing or decryption unless a private key is available.
///
/// If a private key is specified, you don't need to specify a public key file.
/// OpenSSL will auto-create the public key from the private key.
ECKey(std::istream * pPublicKeyStream, std::istream * pPrivateKeyStream = 0, const std::string & privateKeyPassphrase = "");
/// Creates the ECKey, by reading public and private key from the given streams and
/// using the given passphrase for the private key.
///
/// Cannot be used for signing or decryption unless a private key is available.
///
/// If a private key is specified, you don't need to specify a public key file.
/// OpenSSL will auto-create the public key from the private key.
~ECKey();
/// Destroys the ECKey.
ECKeyImpl::Ptr impl() const;
/// Returns the impl object.
static std::string getCurveName(int nid = -1);
/// Returns elliptical curve name corresponding to
/// the given nid; if nid is not found, returns
/// empty string.
///
/// If nid is -1, returns first curve name.
///
/// If no curves are found, returns empty string;
static int getCurveNID(std::string & name);
/// Returns the NID of the specified curve.
///
/// If name is empty, returns the first curve NID
/// and updates the name accordingly.
static bool hasCurve(const std::string & name);
/// Returns true if the named curve is found,
/// false otherwise.
private:
ECKeyImpl::Ptr _pImpl;
};
//
// inlines
//
inline ECKeyImpl::Ptr ECKey::impl() const
{
return _pImpl;
}
inline std::string ECKey::getCurveName(int nid)
{
return ECKeyImpl::getCurveName(nid);
}
inline int ECKey::getCurveNID(std::string & name)
{
return ECKeyImpl::getCurveNID(name);
}
inline bool ECKey::hasCurve(const std::string & name)
{
return ECKeyImpl::hasCurve(name);
}
}
inline std::string ECKey::getCurveName(int nid)
{
return ECKeyImpl::getCurveName(nid);
}
inline int ECKey::getCurveNID(std::string& name)
{
return ECKeyImpl::getCurveNID(name);
}
inline bool ECKey::hasCurve(const std::string& name)
{
return ECKeyImpl::hasCurve(name);
}
} } // namespace Poco::Crypto
} // namespace Poco::Crypto
#endif // Crypto_ECKey_INCLUDED

View File

@ -19,156 +19,155 @@
#define Crypto_ECKeyImplImpl_INCLUDED
#include <istream>
#include <ostream>
#include <vector>
#include <openssl/ec.h>
#include <openssl/objects.h>
#include "Poco/AutoPtr.h"
#include "Poco/Crypto/Crypto.h"
#include "Poco/Crypto/EVPPKey.h"
#include "Poco/Crypto/KeyPairImpl.h"
#include "Poco/Crypto/OpenSSLInitializer.h"
#include "Poco/RefCountedObject.h"
#include "Poco/AutoPtr.h"
#include <istream>
#include <ostream>
#include <vector>
#include <openssl/objects.h>
#include <openssl/ec.h>
namespace Poco {
namespace Crypto {
class X509Certificate;
class PKCS12Container;
class ECKeyImpl: public KeyPairImpl
/// Elliptic Curve key clas implementation.
namespace Poco
{
public:
typedef Poco::AutoPtr<ECKeyImpl> Ptr;
typedef std::vector<unsigned char> ByteVec;
ECKeyImpl(const EVPPKey& key);
/// Constructs ECKeyImpl by extracting the EC key.
ECKeyImpl(const X509Certificate& cert);
/// Constructs ECKeyImpl by extracting the EC public key from the given certificate.
ECKeyImpl(const PKCS12Container& cert);
/// Constructs ECKeyImpl by extracting the EC private key from the given certificate.
ECKeyImpl(int eccGroup);
/// Creates the ECKey of the specified group. Creates a new public/private keypair using the given parameters.
/// Can be used to sign data and verify signatures.
ECKeyImpl(const std::string& publicKeyFile, const std::string& privateKeyFile, const std::string& privateKeyPassphrase);
/// Creates the ECKey, by reading public and private key from the given files and
/// using the given passphrase for the private key. Can only by used for signing if
/// a private key is available.
ECKeyImpl(std::istream* pPublicKeyStream, std::istream* pPrivateKeyStream, const std::string& privateKeyPassphrase);
/// Creates the ECKey. Can only by used for signing if pPrivKey
/// is not null. If a private key file is specified, you don't need to
/// specify a public key file. OpenSSL will auto-create it from the private key.
~ECKeyImpl();
/// Destroys the ECKeyImpl.
EC_KEY* getECKey();
/// Returns the OpenSSL EC key.
const EC_KEY* getECKey() const;
/// Returns the OpenSSL EC key.
int size() const;
/// Returns the EC key length in bits.
int groupId() const;
/// Returns the EC key group integer Id.
std::string groupName() const;
/// Returns the EC key group name.
void save(const std::string& publicKeyFile,
const std::string& privateKeyFile = "",
const std::string& privateKeyPassphrase = "") const;
/// Exports the public and private keys to the given files.
///
/// If an empty filename is specified, the corresponding key
/// is not exported.
void save(std::ostream* pPublicKeyStream,
std::ostream* pPrivateKeyStream = 0,
const std::string& privateKeyPassphrase = "") const;
/// Exports the public and private key to the given streams.
///
/// If a null pointer is passed for a stream, the corresponding
/// key is not exported.
static std::string getCurveName(int nid = -1);
/// Returns elliptical curve name corresponding to
/// the given nid; if nid is not found, returns
/// empty string.
///
/// If nid is -1, returns first curve name.
///
/// If no curves are found, returns empty string;
static int getCurveNID(std::string& name);
/// Returns the NID of the specified curve.
///
/// If name is empty, returns the first curve NID
/// and updates the name accordingly.
static bool hasCurve(const std::string& name);
/// Returns true if the named curve is found,
/// false otherwise.
private:
void checkEC(const std::string& method, const std::string& func) const;
void freeEC();
EC_KEY* _pEC;
};
//
// inlines
//
inline EC_KEY* ECKeyImpl::getECKey()
namespace Crypto
{
return _pEC;
class X509Certificate;
class PKCS12Container;
class ECKeyImpl : public KeyPairImpl
/// Elliptic Curve key clas implementation.
{
public:
typedef Poco::AutoPtr<ECKeyImpl> Ptr;
typedef std::vector<unsigned char> ByteVec;
ECKeyImpl(const EVPPKey & key);
/// Constructs ECKeyImpl by extracting the EC key.
ECKeyImpl(const X509Certificate & cert);
/// Constructs ECKeyImpl by extracting the EC public key from the given certificate.
ECKeyImpl(const PKCS12Container & cert);
/// Constructs ECKeyImpl by extracting the EC private key from the given certificate.
ECKeyImpl(int eccGroup);
/// Creates the ECKey of the specified group. Creates a new public/private keypair using the given parameters.
/// Can be used to sign data and verify signatures.
ECKeyImpl(const std::string & publicKeyFile, const std::string & privateKeyFile, const std::string & privateKeyPassphrase);
/// Creates the ECKey, by reading public and private key from the given files and
/// using the given passphrase for the private key. Can only by used for signing if
/// a private key is available.
ECKeyImpl(std::istream * pPublicKeyStream, std::istream * pPrivateKeyStream, const std::string & privateKeyPassphrase);
/// Creates the ECKey. Can only by used for signing if pPrivKey
/// is not null. If a private key file is specified, you don't need to
/// specify a public key file. OpenSSL will auto-create it from the private key.
~ECKeyImpl();
/// Destroys the ECKeyImpl.
EC_KEY * getECKey();
/// Returns the OpenSSL EC key.
const EC_KEY * getECKey() const;
/// Returns the OpenSSL EC key.
int size() const;
/// Returns the EC key length in bits.
int groupId() const;
/// Returns the EC key group integer Id.
std::string groupName() const;
/// Returns the EC key group name.
void save(const std::string & publicKeyFile, const std::string & privateKeyFile = "", const std::string & privateKeyPassphrase = "")
const;
/// Exports the public and private keys to the given files.
///
/// If an empty filename is specified, the corresponding key
/// is not exported.
void
save(std::ostream * pPublicKeyStream, std::ostream * pPrivateKeyStream = 0, const std::string & privateKeyPassphrase = "") const;
/// Exports the public and private key to the given streams.
///
/// If a null pointer is passed for a stream, the corresponding
/// key is not exported.
static std::string getCurveName(int nid = -1);
/// Returns elliptical curve name corresponding to
/// the given nid; if nid is not found, returns
/// empty string.
///
/// If nid is -1, returns first curve name.
///
/// If no curves are found, returns empty string;
static int getCurveNID(std::string & name);
/// Returns the NID of the specified curve.
///
/// If name is empty, returns the first curve NID
/// and updates the name accordingly.
static bool hasCurve(const std::string & name);
/// Returns true if the named curve is found,
/// false otherwise.
private:
void checkEC(const std::string & method, const std::string & func) const;
void freeEC();
EC_KEY * _pEC;
};
//
// inlines
//
inline EC_KEY * ECKeyImpl::getECKey()
{
return _pEC;
}
inline const EC_KEY * ECKeyImpl::getECKey() const
{
return _pEC;
}
inline std::string ECKeyImpl::groupName() const
{
return OBJ_nid2sn(groupId());
}
inline void
ECKeyImpl::save(const std::string & publicKeyFile, const std::string & privateKeyFile, const std::string & privateKeyPassphrase) const
{
EVPPKey(_pEC).save(publicKeyFile, privateKeyFile, privateKeyPassphrase);
}
inline void
ECKeyImpl::save(std::ostream * pPublicKeyStream, std::ostream * pPrivateKeyStream, const std::string & privateKeyPassphrase) const
{
EVPPKey(_pEC).save(pPublicKeyStream, pPrivateKeyStream, privateKeyPassphrase);
}
}
inline const EC_KEY* ECKeyImpl::getECKey() const
{
return _pEC;
}
inline std::string ECKeyImpl::groupName() const
{
return OBJ_nid2sn(groupId());
}
inline void ECKeyImpl::save(const std::string& publicKeyFile,
const std::string& privateKeyFile,
const std::string& privateKeyPassphrase) const
{
EVPPKey(_pEC).save(publicKeyFile, privateKeyFile, privateKeyPassphrase);
}
inline void ECKeyImpl::save(std::ostream* pPublicKeyStream,
std::ostream* pPrivateKeyStream,
const std::string& privateKeyPassphrase) const
{
EVPPKey(_pEC).save(pPublicKeyStream, pPrivateKeyStream, privateKeyPassphrase);
}
} } // namespace Poco::Crypto
} // namespace Poco::Crypto
#endif // Crypto_ECKeyImplImpl_INCLUDED

View File

@ -19,336 +19,351 @@
#define Crypto_EVPPKeyImpl_INCLUDED
#include <sstream>
#include <typeinfo>
#include <openssl/ec.h>
#include <openssl/evp.h>
#include <openssl/pem.h>
#include <openssl/rsa.h>
#include "Poco/Crypto/Crypto.h"
#include "Poco/Crypto/CryptoException.h"
#include "Poco/StreamCopier.h"
#include <openssl/ec.h>
#include <openssl/rsa.h>
#include <openssl/evp.h>
#include <openssl/pem.h>
#include <sstream>
#include <typeinfo>
namespace Poco {
namespace Crypto {
class ECKey;
class RSAKey;
class Crypto_API EVPPKey
/// Utility class for conversion of native keys to EVP.
/// Currently, only RSA and EC keys are supported.
namespace Poco
{
namespace Crypto
{
public:
explicit EVPPKey(const std::string& ecCurveName);
/// Constructs EVPPKey from ECC curve name.
///
/// Only EC keys can be wrapped by an EVPPKey
/// created using this constructor.
explicit EVPPKey(const char* ecCurveName);
/// Constructs EVPPKey from ECC curve name.
///
/// Only EC keys can be wrapped by an EVPPKey
/// created using this constructor.
explicit EVPPKey(EVP_PKEY* pEVPPKey);
/// Constructs EVPPKey from EVP_PKEY pointer.
/// The content behind the supplied pointer is internally duplicated.
class ECKey;
class RSAKey;
template<typename K>
explicit EVPPKey(K* pKey): _pEVPPKey(EVP_PKEY_new())
/// Constructs EVPPKey from a "native" OpenSSL (RSA or EC_KEY),
/// or a Poco wrapper (RSAKey, ECKey) key pointer.
{
if (!_pEVPPKey) throw OpenSSLException();
setKey(pKey);
}
EVPPKey(const std::string& publicKeyFile, const std::string& privateKeyFile, const std::string& privateKeyPassphrase = "");
/// Creates the EVPPKey, by reading public and private key from the given files and
/// using the given passphrase for the private key. Can only by used for signing if
/// a private key is available.
class Crypto_API EVPPKey
/// Utility class for conversion of native keys to EVP.
/// Currently, only RSA and EC keys are supported.
{
public:
explicit EVPPKey(const std::string & ecCurveName);
/// Constructs EVPPKey from ECC curve name.
///
/// Only EC keys can be wrapped by an EVPPKey
/// created using this constructor.
EVPPKey(std::istream* pPublicKeyStream, std::istream* pPrivateKeyStream, const std::string& privateKeyPassphrase = "");
/// Creates the EVPPKey. Can only by used for signing if pPrivKey
/// is not null. If a private key file is specified, you don't need to
/// specify a public key file. OpenSSL will auto-create it from the private key.
explicit EVPPKey(const char * ecCurveName);
/// Constructs EVPPKey from ECC curve name.
///
/// Only EC keys can be wrapped by an EVPPKey
/// created using this constructor.
EVPPKey(const EVPPKey& other);
/// Copy constructor.
explicit EVPPKey(EVP_PKEY * pEVPPKey);
/// Constructs EVPPKey from EVP_PKEY pointer.
/// The content behind the supplied pointer is internally duplicated.
EVPPKey& operator=(const EVPPKey& other);
/// Assignment operator.
template <typename K>
explicit EVPPKey(K * pKey) : _pEVPPKey(EVP_PKEY_new())
/// Constructs EVPPKey from a "native" OpenSSL (RSA or EC_KEY),
/// or a Poco wrapper (RSAKey, ECKey) key pointer.
{
if (!_pEVPPKey)
throw OpenSSLException();
setKey(pKey);
}
EVPPKey(const std::string & publicKeyFile, const std::string & privateKeyFile, const std::string & privateKeyPassphrase = "");
/// Creates the EVPPKey, by reading public and private key from the given files and
/// using the given passphrase for the private key. Can only by used for signing if
/// a private key is available.
EVPPKey(std::istream * pPublicKeyStream, std::istream * pPrivateKeyStream, const std::string & privateKeyPassphrase = "");
/// Creates the EVPPKey. Can only by used for signing if pPrivKey
/// is not null. If a private key file is specified, you don't need to
/// specify a public key file. OpenSSL will auto-create it from the private key.
EVPPKey(const EVPPKey & other);
/// Copy constructor.
EVPPKey & operator=(const EVPPKey & other);
/// Assignment operator.
#ifdef POCO_ENABLE_CPP11
EVPPKey(EVPPKey&& other);
/// Move constructor.
EVPPKey(EVPPKey && other);
/// Move constructor.
EVPPKey& operator=(EVPPKey&& other);
/// Assignment move operator.
EVPPKey & operator=(EVPPKey && other);
/// Assignment move operator.
#endif // POCO_ENABLE_CPP11
~EVPPKey();
/// Destroys the EVPPKey.
~EVPPKey();
/// Destroys the EVPPKey.
bool operator == (const EVPPKey& other) const;
/// Comparison operator.
/// Returns true if public key components and parameters
/// of the other key are equal to this key.
///
/// Works as expected when one key contains only public key,
/// while the other one contains private (thus also public) key.
bool operator==(const EVPPKey & other) const;
/// Comparison operator.
/// Returns true if public key components and parameters
/// of the other key are equal to this key.
///
/// Works as expected when one key contains only public key,
/// while the other one contains private (thus also public) key.
bool operator != (const EVPPKey& other) const;
/// Comparison operator.
/// Returns true if public key components and parameters
/// of the other key are different from this key.
///
/// Works as expected when one key contains only public key,
/// while the other one contains private (thus also public) key.
bool operator!=(const EVPPKey & other) const;
/// Comparison operator.
/// Returns true if public key components and parameters
/// of the other key are different from this key.
///
/// Works as expected when one key contains only public key,
/// while the other one contains private (thus also public) key.
void save(const std::string& publicKeyFile, const std::string& privateKeyFile = "", const std::string& privateKeyPassphrase = "") const;
/// Exports the public and/or private keys to the given files.
///
/// If an empty filename is specified, the corresponding key
/// is not exported.
void save(const std::string & publicKeyFile, const std::string & privateKeyFile = "", const std::string & privateKeyPassphrase = "")
const;
/// Exports the public and/or private keys to the given files.
///
/// If an empty filename is specified, the corresponding key
/// is not exported.
void save(std::ostream* pPublicKeyStream, std::ostream* pPrivateKeyStream = 0, const std::string& privateKeyPassphrase = "") const;
/// Exports the public and/or private key to the given streams.
///
/// If a null pointer is passed for a stream, the corresponding
/// key is not exported.
void
save(std::ostream * pPublicKeyStream, std::ostream * pPrivateKeyStream = 0, const std::string & privateKeyPassphrase = "") const;
/// Exports the public and/or private key to the given streams.
///
/// If a null pointer is passed for a stream, the corresponding
/// key is not exported.
int type() const;
/// Returns the EVPPKey type NID.
int type() const;
/// Returns the EVPPKey type NID.
bool isSupported(int type) const;
/// Returns true if OpenSSL type is supported
bool isSupported(int type) const;
/// Returns true if OpenSSL type is supported
operator const EVP_PKEY*() const;
/// Returns const pointer to the OpenSSL EVP_PKEY structure.
operator const EVP_PKEY *() const;
/// Returns const pointer to the OpenSSL EVP_PKEY structure.
operator EVP_PKEY*();
/// Returns pointer to the OpenSSL EVP_PKEY structure.
operator EVP_PKEY *();
/// Returns pointer to the OpenSSL EVP_PKEY structure.
static EVP_PKEY* duplicate(const EVP_PKEY* pFromKey, EVP_PKEY** pToKey);
/// Duplicates pFromKey into *pToKey and returns
// the pointer to duplicated EVP_PKEY.
static EVP_PKEY * duplicate(const EVP_PKEY * pFromKey, EVP_PKEY ** pToKey);
/// Duplicates pFromKey into *pToKey and returns
// the pointer to duplicated EVP_PKEY.
private:
EVPPKey();
private:
EVPPKey();
static int type(const EVP_PKEY* pEVPPKey);
void newECKey(const char* group);
void duplicate(EVP_PKEY* pEVPPKey);
static int type(const EVP_PKEY * pEVPPKey);
void newECKey(const char * group);
void duplicate(EVP_PKEY * pEVPPKey);
void setKey(ECKey* pKey);
void setKey(RSAKey* pKey);
void setKey(EC_KEY* pKey);
void setKey(RSA* pKey);
static int passCB(char* buf, int size, int, void* pass);
void setKey(ECKey * pKey);
void setKey(RSAKey * pKey);
void setKey(EC_KEY * pKey);
void setKey(RSA * pKey);
static int passCB(char * buf, int size, int, void * pass);
typedef EVP_PKEY* (*PEM_read_FILE_Key_fn)(FILE*, EVP_PKEY**, pem_password_cb*, void*);
typedef EVP_PKEY* (*PEM_read_BIO_Key_fn)(BIO*, EVP_PKEY**, pem_password_cb*, void*);
typedef void* (*EVP_PKEY_get_Key_fn)(EVP_PKEY*);
typedef EVP_PKEY * (*PEM_read_FILE_Key_fn)(FILE *, EVP_PKEY **, pem_password_cb *, void *);
typedef EVP_PKEY * (*PEM_read_BIO_Key_fn)(BIO *, EVP_PKEY **, pem_password_cb *, void *);
typedef void * (*EVP_PKEY_get_Key_fn)(EVP_PKEY *);
// The following load*() functions are used by both native and EVP_PKEY type key
// loading from BIO/FILE.
// When used for EVP key loading, getFunc is null (ie. native key is not extracted
// from the loaded EVP_PKEY).
template <typename K, typename F>
static bool loadKey(K** ppKey,
PEM_read_FILE_Key_fn readFunc,
F getFunc,
const std::string& keyFile,
const std::string& pass = "")
{
poco_assert_dbg (((typeid(K*) == typeid(RSA*) || typeid(K*) == typeid(EC_KEY*)) && getFunc) ||
((typeid(K*) == typeid(EVP_PKEY*)) && !getFunc));
poco_check_ptr (ppKey);
poco_assert_dbg (!*ppKey);
// The following load*() functions are used by both native and EVP_PKEY type key
// loading from BIO/FILE.
// When used for EVP key loading, getFunc is null (ie. native key is not extracted
// from the loaded EVP_PKEY).
template <typename K, typename F>
static bool
loadKey(K ** ppKey, PEM_read_FILE_Key_fn readFunc, F getFunc, const std::string & keyFile, const std::string & pass = "")
{
poco_assert_dbg(
((typeid(K *) == typeid(RSA *) || typeid(K *) == typeid(EC_KEY *)) && getFunc)
|| ((typeid(K *) == typeid(EVP_PKEY *)) && !getFunc));
poco_check_ptr(ppKey);
poco_assert_dbg(!*ppKey);
FILE* pFile = 0;
if (!keyFile.empty())
{
if (!getFunc) *ppKey = (K*)EVP_PKEY_new();
EVP_PKEY* pKey = getFunc ? EVP_PKEY_new() : (EVP_PKEY*)*ppKey;
if (pKey)
{
pFile = fopen(keyFile.c_str(), "r");
if (pFile)
{
pem_password_cb* pCB = pass.empty() ? (pem_password_cb*)0 : &passCB;
void* pPassword = pass.empty() ? (void*)0 : (void*)pass.c_str();
if (readFunc(pFile, &pKey, pCB, pPassword))
{
fclose(pFile); pFile = 0;
if(getFunc)
{
*ppKey = (K*)getFunc(pKey);
EVP_PKEY_free(pKey);
}
else
{
poco_assert_dbg (typeid(K*) == typeid(EVP_PKEY*));
*ppKey = (K*)pKey;
}
if(!*ppKey) goto error;
return true;
}
goto error;
}
else
{
if (getFunc) EVP_PKEY_free(pKey);
throw IOException("ECKeyImpl, cannot open file", keyFile);
}
}
else goto error;
}
return false;
FILE * pFile = 0;
if (!keyFile.empty())
{
if (!getFunc)
*ppKey = (K *)EVP_PKEY_new();
EVP_PKEY * pKey = getFunc ? EVP_PKEY_new() : (EVP_PKEY *)*ppKey;
if (pKey)
{
pFile = fopen(keyFile.c_str(), "r");
if (pFile)
{
pem_password_cb * pCB = pass.empty() ? (pem_password_cb *)0 : &passCB;
void * pPassword = pass.empty() ? (void *)0 : (void *)pass.c_str();
if (readFunc(pFile, &pKey, pCB, pPassword))
{
fclose(pFile);
pFile = 0;
if (getFunc)
{
*ppKey = (K *)getFunc(pKey);
EVP_PKEY_free(pKey);
}
else
{
poco_assert_dbg(typeid(K *) == typeid(EVP_PKEY *));
*ppKey = (K *)pKey;
}
if (!*ppKey)
goto error;
return true;
}
goto error;
}
else
{
if (getFunc)
EVP_PKEY_free(pKey);
throw IOException("ECKeyImpl, cannot open file", keyFile);
}
}
else
goto error;
}
return false;
error:
if (pFile) fclose(pFile);
throw OpenSSLException("EVPKey::loadKey(string)");
}
error:
if (pFile)
fclose(pFile);
throw OpenSSLException("EVPKey::loadKey(string)");
}
template <typename K, typename F>
static bool loadKey(K** ppKey,
PEM_read_BIO_Key_fn readFunc,
F getFunc,
std::istream* pIstr,
const std::string& pass = "")
{
poco_assert_dbg (((typeid(K*) == typeid(RSA*) || typeid(K*) == typeid(EC_KEY*)) && getFunc) ||
((typeid(K*) == typeid(EVP_PKEY*)) && !getFunc));
poco_check_ptr(ppKey);
poco_assert_dbg(!*ppKey);
template <typename K, typename F>
static bool loadKey(K ** ppKey, PEM_read_BIO_Key_fn readFunc, F getFunc, std::istream * pIstr, const std::string & pass = "")
{
poco_assert_dbg(
((typeid(K *) == typeid(RSA *) || typeid(K *) == typeid(EC_KEY *)) && getFunc)
|| ((typeid(K *) == typeid(EVP_PKEY *)) && !getFunc));
poco_check_ptr(ppKey);
poco_assert_dbg(!*ppKey);
BIO* pBIO = 0;
if (pIstr)
{
std::ostringstream ostr;
Poco::StreamCopier::copyStream(*pIstr, ostr);
std::string key = ostr.str();
pBIO = BIO_new_mem_buf(const_cast<char*>(key.data()), static_cast<int>(key.size()));
if (pBIO)
{
if (!getFunc) *ppKey = (K*)EVP_PKEY_new();
EVP_PKEY* pKey = getFunc ? EVP_PKEY_new() : (EVP_PKEY*)*ppKey;
if (pKey)
{
pem_password_cb* pCB = pass.empty() ? (pem_password_cb*)0 : &passCB;
void* pPassword = pass.empty() ? (void*)0 : (void*)pass.c_str();
if (readFunc(pBIO, &pKey, pCB, pPassword))
{
BIO_free(pBIO); pBIO = 0;
if (getFunc)
{
*ppKey = (K*)getFunc(pKey);
EVP_PKEY_free(pKey);
}
else
{
poco_assert_dbg (typeid(K*) == typeid(EVP_PKEY*));
*ppKey = (K*)pKey;
}
if (!*ppKey) goto error;
return true;
}
if (getFunc) EVP_PKEY_free(pKey);
goto error;
}
else goto error;
}
else goto error;
}
return false;
BIO * pBIO = 0;
if (pIstr)
{
std::ostringstream ostr;
Poco::StreamCopier::copyStream(*pIstr, ostr);
std::string key = ostr.str();
pBIO = BIO_new_mem_buf(const_cast<char *>(key.data()), static_cast<int>(key.size()));
if (pBIO)
{
if (!getFunc)
*ppKey = (K *)EVP_PKEY_new();
EVP_PKEY * pKey = getFunc ? EVP_PKEY_new() : (EVP_PKEY *)*ppKey;
if (pKey)
{
pem_password_cb * pCB = pass.empty() ? (pem_password_cb *)0 : &passCB;
void * pPassword = pass.empty() ? (void *)0 : (void *)pass.c_str();
if (readFunc(pBIO, &pKey, pCB, pPassword))
{
BIO_free(pBIO);
pBIO = 0;
if (getFunc)
{
*ppKey = (K *)getFunc(pKey);
EVP_PKEY_free(pKey);
}
else
{
poco_assert_dbg(typeid(K *) == typeid(EVP_PKEY *));
*ppKey = (K *)pKey;
}
if (!*ppKey)
goto error;
return true;
}
if (getFunc)
EVP_PKEY_free(pKey);
goto error;
}
else
goto error;
}
else
goto error;
}
return false;
error:
if (pBIO) BIO_free(pBIO);
throw OpenSSLException("EVPKey::loadKey(stream)");
}
error:
if (pBIO)
BIO_free(pBIO);
throw OpenSSLException("EVPKey::loadKey(stream)");
}
EVP_PKEY* _pEVPPKey;
EVP_PKEY * _pEVPPKey;
friend class ECKeyImpl;
friend class RSAKeyImpl;
};
friend class ECKeyImpl;
friend class RSAKeyImpl;
};
//
// inlines
//
//
// inlines
//
inline bool EVPPKey::operator==(const EVPPKey & other) const
{
poco_check_ptr(other._pEVPPKey);
poco_check_ptr(_pEVPPKey);
return (1 == EVP_PKEY_cmp(_pEVPPKey, other._pEVPPKey));
}
inline bool EVPPKey::operator!=(const EVPPKey & other) const
{
return !(other == *this);
}
inline int EVPPKey::type(const EVP_PKEY * pEVPPKey)
{
if (!pEVPPKey)
return NID_undef;
return EVP_PKEY_type(EVP_PKEY_id(pEVPPKey));
}
inline int EVPPKey::type() const
{
return type(_pEVPPKey);
}
inline bool EVPPKey::isSupported(int type) const
{
return type == EVP_PKEY_EC || type == EVP_PKEY_RSA;
}
inline EVPPKey::operator const EVP_PKEY *() const
{
return _pEVPPKey;
}
inline EVPPKey::operator EVP_PKEY *()
{
return _pEVPPKey;
}
inline void EVPPKey::setKey(EC_KEY * pKey)
{
if (!EVP_PKEY_set1_EC_KEY(_pEVPPKey, pKey))
throw OpenSSLException();
}
inline void EVPPKey::setKey(RSA * pKey)
{
if (!EVP_PKEY_set1_RSA(_pEVPPKey, pKey))
throw OpenSSLException();
}
inline bool EVPPKey::operator == (const EVPPKey& other) const
{
poco_check_ptr (other._pEVPPKey);
poco_check_ptr (_pEVPPKey);
return (1 == EVP_PKEY_cmp(_pEVPPKey, other._pEVPPKey));
}
inline bool EVPPKey::operator != (const EVPPKey& other) const
{
return !(other == *this);
}
inline int EVPPKey::type(const EVP_PKEY* pEVPPKey)
{
if (!pEVPPKey) return NID_undef;
return EVP_PKEY_type(EVP_PKEY_id(pEVPPKey));
}
inline int EVPPKey::type() const
{
return type(_pEVPPKey);
}
inline bool EVPPKey::isSupported(int type) const
{
return type == EVP_PKEY_EC || type == EVP_PKEY_RSA;
}
inline EVPPKey::operator const EVP_PKEY*() const
{
return _pEVPPKey;
}
inline EVPPKey::operator EVP_PKEY*()
{
return _pEVPPKey;
}
inline void EVPPKey::setKey(EC_KEY* pKey)
{
if (!EVP_PKEY_set1_EC_KEY(_pEVPPKey, pKey))
throw OpenSSLException();
}
inline void EVPPKey::setKey(RSA* pKey)
{
if (!EVP_PKEY_set1_RSA(_pEVPPKey, pKey))
throw OpenSSLException();
}
} } // namespace Poco::Crypto
} // namespace Poco::Crypto
#endif // Crypto_EVPPKeyImpl_INCLUDED

View File

@ -23,111 +23,114 @@
#include "Poco/Crypto/KeyPairImpl.h"
namespace Poco {
namespace Crypto {
class X509Certificate;
class Crypto_API KeyPair
/// This is a parent class for classes storing a key pair, consisting
/// of private and public key. Storage of the private key is optional.
///
/// If a private key is available, the KeyPair can be
/// used for decrypting data (encrypted with the public key)
/// or computing secure digital signatures.
namespace Poco
{
public:
enum Type
{
KT_RSA = KeyPairImpl::KT_RSA_IMPL,
KT_EC = KeyPairImpl::KT_EC_IMPL
};
explicit KeyPair(KeyPairImpl::Ptr pKeyPairImpl = 0);
/// Extracts the RSA public key from the given certificate.
virtual ~KeyPair();
/// Destroys the KeyPair.
virtual int size() const;
/// Returns the RSA modulus size.
virtual void save(const std::string& publicKeyPairFile,
const std::string& privateKeyPairFile = "",
const std::string& privateKeyPairPassphrase = "") const;
/// Exports the public and private keys to the given files.
///
/// If an empty filename is specified, the corresponding key
/// is not exported.
virtual void save(std::ostream* pPublicKeyPairStream,
std::ostream* pPrivateKeyPairStream = 0,
const std::string& privateKeyPairPassphrase = "") const;
/// Exports the public and private key to the given streams.
///
/// If a null pointer is passed for a stream, the corresponding
/// key is not exported.
KeyPairImpl::Ptr impl() const;
/// Returns the impl object.
const std::string& name() const;
/// Returns key pair name
Type type() const;
/// Returns key pair type
private:
KeyPairImpl::Ptr _pImpl;
};
//
// inlines
//
inline int KeyPair::size() const
namespace Crypto
{
return _pImpl->size();
class X509Certificate;
class Crypto_API KeyPair
/// This is a parent class for classes storing a key pair, consisting
/// of private and public key. Storage of the private key is optional.
///
/// If a private key is available, the KeyPair can be
/// used for decrypting data (encrypted with the public key)
/// or computing secure digital signatures.
{
public:
enum Type
{
KT_RSA = KeyPairImpl::KT_RSA_IMPL,
KT_EC = KeyPairImpl::KT_EC_IMPL
};
explicit KeyPair(KeyPairImpl::Ptr pKeyPairImpl = 0);
/// Extracts the RSA public key from the given certificate.
virtual ~KeyPair();
/// Destroys the KeyPair.
virtual int size() const;
/// Returns the RSA modulus size.
virtual void save(
const std::string & publicKeyPairFile,
const std::string & privateKeyPairFile = "",
const std::string & privateKeyPairPassphrase = "") const;
/// Exports the public and private keys to the given files.
///
/// If an empty filename is specified, the corresponding key
/// is not exported.
virtual void save(
std::ostream * pPublicKeyPairStream,
std::ostream * pPrivateKeyPairStream = 0,
const std::string & privateKeyPairPassphrase = "") const;
/// Exports the public and private key to the given streams.
///
/// If a null pointer is passed for a stream, the corresponding
/// key is not exported.
KeyPairImpl::Ptr impl() const;
/// Returns the impl object.
const std::string & name() const;
/// Returns key pair name
Type type() const;
/// Returns key pair type
private:
KeyPairImpl::Ptr _pImpl;
};
//
// inlines
//
inline int KeyPair::size() const
{
return _pImpl->size();
}
inline void
KeyPair::save(const std::string & publicKeyFile, const std::string & privateKeyFile, const std::string & privateKeyPassphrase) const
{
_pImpl->save(publicKeyFile, privateKeyFile, privateKeyPassphrase);
}
inline void
KeyPair::save(std::ostream * pPublicKeyStream, std::ostream * pPrivateKeyStream, const std::string & privateKeyPassphrase) const
{
_pImpl->save(pPublicKeyStream, pPrivateKeyStream, privateKeyPassphrase);
}
inline const std::string & KeyPair::name() const
{
return _pImpl->name();
}
inline KeyPairImpl::Ptr KeyPair::impl() const
{
return _pImpl;
}
inline KeyPair::Type KeyPair::type() const
{
return (KeyPair::Type)impl()->type();
}
}
inline void KeyPair::save(const std::string& publicKeyFile,
const std::string& privateKeyFile,
const std::string& privateKeyPassphrase) const
{
_pImpl->save(publicKeyFile, privateKeyFile, privateKeyPassphrase);
}
inline void KeyPair::save(std::ostream* pPublicKeyStream,
std::ostream* pPrivateKeyStream,
const std::string& privateKeyPassphrase) const
{
_pImpl->save(pPublicKeyStream, pPrivateKeyStream, privateKeyPassphrase);
}
inline const std::string& KeyPair::name() const
{
return _pImpl->name();
}
inline KeyPairImpl::Ptr KeyPair::impl() const
{
return _pImpl;
}
inline KeyPair::Type KeyPair::type() const
{
return (KeyPair::Type)impl()->type();
}
} } // namespace Poco::Crypto
} // namespace Poco::Crypto
#endif // Crypto_KeyPair_INCLUDED

View File

@ -19,89 +19,92 @@
#define Crypto_KeyPairImplImpl_INCLUDED
#include <string>
#include <vector>
#include "Poco/AutoPtr.h"
#include "Poco/Crypto/Crypto.h"
#include "Poco/Crypto/OpenSSLInitializer.h"
#include "Poco/RefCountedObject.h"
#include "Poco/AutoPtr.h"
#include <string>
#include <vector>
namespace Poco {
namespace Crypto {
class KeyPairImpl: public Poco::RefCountedObject
/// Class KeyPairImpl
namespace Poco
{
public:
enum Type
{
KT_RSA_IMPL = 0,
KT_EC_IMPL
};
typedef Poco::AutoPtr<KeyPairImpl> Ptr;
typedef std::vector<unsigned char> ByteVec;
KeyPairImpl(const std::string& name, Type type);
/// Create KeyPairImpl with specified type and name.
virtual ~KeyPairImpl();
/// Destroys the KeyPairImpl.
virtual int size() const = 0;
/// Returns the key size.
virtual void save(const std::string& publicKeyFile,
const std::string& privateKeyFile = "",
const std::string& privateKeyPassphrase = "") const = 0;
/// Exports the public and private keys to the given files.
///
/// If an empty filename is specified, the corresponding key
/// is not exported.
virtual void save(std::ostream* pPublicKeyStream,
std::ostream* pPrivateKeyStream = 0,
const std::string& privateKeyPassphrase = "") const = 0;
/// Exports the public and private key to the given streams.
///
/// If a null pointer is passed for a stream, the corresponding
/// key is not exported.
const std::string& name() const;
/// Returns key pair name
Type type() const;
/// Returns key pair type
private:
KeyPairImpl();
std::string _name;
Type _type;
OpenSSLInitializer _openSSLInitializer;
};
//
// inlines
//
inline const std::string& KeyPairImpl::name() const
namespace Crypto
{
return _name;
class KeyPairImpl : public Poco::RefCountedObject
/// Class KeyPairImpl
{
public:
enum Type
{
KT_RSA_IMPL = 0,
KT_EC_IMPL
};
typedef Poco::AutoPtr<KeyPairImpl> Ptr;
typedef std::vector<unsigned char> ByteVec;
KeyPairImpl(const std::string & name, Type type);
/// Create KeyPairImpl with specified type and name.
virtual ~KeyPairImpl();
/// Destroys the KeyPairImpl.
virtual int size() const = 0;
/// Returns the key size.
virtual void save(
const std::string & publicKeyFile,
const std::string & privateKeyFile = "",
const std::string & privateKeyPassphrase = "") const = 0;
/// Exports the public and private keys to the given files.
///
/// If an empty filename is specified, the corresponding key
/// is not exported.
virtual void save(
std::ostream * pPublicKeyStream, std::ostream * pPrivateKeyStream = 0, const std::string & privateKeyPassphrase = "") const = 0;
/// Exports the public and private key to the given streams.
///
/// If a null pointer is passed for a stream, the corresponding
/// key is not exported.
const std::string & name() const;
/// Returns key pair name
Type type() const;
/// Returns key pair type
private:
KeyPairImpl();
std::string _name;
Type _type;
OpenSSLInitializer _openSSLInitializer;
};
//
// inlines
//
inline const std::string & KeyPairImpl::name() const
{
return _name;
}
inline KeyPairImpl::Type KeyPairImpl::type() const
{
return _type;
}
}
inline KeyPairImpl::Type KeyPairImpl::type() const
{
return _type;
}
} } // namespace Poco::Crypto
} // namespace Poco::Crypto
#endif // Crypto_KeyPairImplImpl_INCLUDED

View File

@ -18,98 +18,100 @@
#define Crypto_OpenSSLInitializer_INCLUDED
#include <openssl/crypto.h>
#include "Poco/AtomicCounter.h"
#include "Poco/Crypto/Crypto.h"
#include "Poco/Mutex.h"
#include "Poco/AtomicCounter.h"
#include <openssl/crypto.h>
#if defined(OPENSSL_FIPS) && OPENSSL_VERSION_NUMBER < 0x010001000L
#include <openssl/fips.h>
# include <openssl/fips.h>
#endif
extern "C"
extern "C" {
struct CRYPTO_dynlock_value
{
struct CRYPTO_dynlock_value
{
Poco::FastMutex _mutex;
};
}
namespace Poco {
namespace Crypto {
class Crypto_API OpenSSLInitializer
/// Initializes the OpenSSL library.
///
/// The class ensures the earliest initialization and the
/// latest shutdown of the OpenSSL library.
{
public:
OpenSSLInitializer();
/// Automatically initialize OpenSSL on startup.
~OpenSSLInitializer();
/// Automatically shut down OpenSSL on exit.
static void initialize();
/// Initializes the OpenSSL machinery.
static void uninitialize();
/// Shuts down the OpenSSL machinery.
static bool isFIPSEnabled();
// Returns true if FIPS mode is enabled, false otherwise.
static void enableFIPSMode(bool enabled);
// Enable or disable FIPS mode. If FIPS is not available, this method doesn't do anything.
protected:
enum
{
SEEDSIZE = 256
};
// OpenSSL multithreading support
static void lock(int mode, int n, const char* file, int line);
static unsigned long id();
static struct CRYPTO_dynlock_value* dynlockCreate(const char* file, int line);
static void dynlock(int mode, struct CRYPTO_dynlock_value* lock, const char* file, int line);
static void dynlockDestroy(struct CRYPTO_dynlock_value* lock, const char* file, int line);
private:
static Poco::FastMutex* _mutexes;
static Poco::AtomicCounter _rc;
Poco::FastMutex _mutex;
};
}
//
// inlines
//
inline bool OpenSSLInitializer::isFIPSEnabled()
namespace Poco
{
namespace Crypto
{
class Crypto_API OpenSSLInitializer
/// Initializes the OpenSSL library.
///
/// The class ensures the earliest initialization and the
/// latest shutdown of the OpenSSL library.
{
public:
OpenSSLInitializer();
/// Automatically initialize OpenSSL on startup.
~OpenSSLInitializer();
/// Automatically shut down OpenSSL on exit.
static void initialize();
/// Initializes the OpenSSL machinery.
static void uninitialize();
/// Shuts down the OpenSSL machinery.
static bool isFIPSEnabled();
// Returns true if FIPS mode is enabled, false otherwise.
static void enableFIPSMode(bool enabled);
// Enable or disable FIPS mode. If FIPS is not available, this method doesn't do anything.
protected:
enum
{
SEEDSIZE = 256
};
// OpenSSL multithreading support
static void lock(int mode, int n, const char * file, int line);
static unsigned long id();
static struct CRYPTO_dynlock_value * dynlockCreate(const char * file, int line);
static void dynlock(int mode, struct CRYPTO_dynlock_value * lock, const char * file, int line);
static void dynlockDestroy(struct CRYPTO_dynlock_value * lock, const char * file, int line);
private:
static Poco::FastMutex * _mutexes;
static Poco::AtomicCounter _rc;
};
//
// inlines
//
inline bool OpenSSLInitializer::isFIPSEnabled()
{
#ifdef OPENSSL_FIPS
return FIPS_mode() ? true : false;
return FIPS_mode() ? true : false;
#else
return false;
return false;
#endif
}
}
#ifdef OPENSSL_FIPS
inline void OpenSSLInitializer::enableFIPSMode(bool enabled)
{
FIPS_mode_set(enabled);
}
inline void OpenSSLInitializer::enableFIPSMode(bool enabled)
{
FIPS_mode_set(enabled);
}
#else
inline void OpenSSLInitializer::enableFIPSMode(bool /*enabled*/)
{
}
inline void OpenSSLInitializer::enableFIPSMode(bool /*enabled*/)
{
}
#endif
} } // namespace Poco::Crypto
}
} // namespace Poco::Crypto
#endif // Crypto_OpenSSLInitializer_INCLUDED

View File

@ -18,142 +18,145 @@
#define Crypto_PKCS12Container_INCLUDED
#include <istream>
#include <memory>
#include <openssl/pkcs12.h>
#include "Poco/Crypto/Crypto.h"
#include "Poco/Crypto/EVPPKey.h"
#include "Poco/Crypto/OpenSSLInitializer.h"
#include "Poco/Crypto/X509Certificate.h"
#include "Poco/Crypto/EVPPKey.h"
#include "Poco/Path.h"
#include <memory>
#include <istream>
#include <openssl/pkcs12.h>
namespace Poco {
namespace Crypto {
class Crypto_API PKCS12Container
/// This class implements PKCS#12 container functionality.
namespace Poco
{
namespace Crypto
{
public:
typedef X509Certificate::List CAList;
typedef std::vector<std::string> CANameList;
explicit PKCS12Container(std::istream& istr, const std::string& password = "");
/// Creates the PKCS12Container object from a stream.
explicit PKCS12Container(const std::string& path, const std::string& password = "");
/// Creates the PKCS12Container object from a file.
class Crypto_API PKCS12Container
/// This class implements PKCS#12 container functionality.
{
public:
typedef X509Certificate::List CAList;
typedef std::vector<std::string> CANameList;
PKCS12Container(const PKCS12Container& cont);
/// Copy constructor.
explicit PKCS12Container(std::istream & istr, const std::string & password = "");
/// Creates the PKCS12Container object from a stream.
PKCS12Container& operator = (const PKCS12Container& cont);
/// Assignment operator.
explicit PKCS12Container(const std::string & path, const std::string & password = "");
/// Creates the PKCS12Container object from a file.
PKCS12Container(const PKCS12Container & cont);
/// Copy constructor.
PKCS12Container & operator=(const PKCS12Container & cont);
/// Assignment operator.
#ifdef POCO_ENABLE_CPP11
PKCS12Container(PKCS12Container&& cont);
/// Move constructor.
PKCS12Container(PKCS12Container && cont);
/// Move constructor.
PKCS12Container& operator = (PKCS12Container&& cont);
/// Move assignment operator.
PKCS12Container & operator=(PKCS12Container && cont);
/// Move assignment operator.
#endif // POCO_ENABLE_CPP11
~PKCS12Container();
/// Destroys the PKCS12Container.
~PKCS12Container();
/// Destroys the PKCS12Container.
bool hasKey() const;
/// Returns true if container contains the key.
bool hasKey() const;
/// Returns true if container contains the key.
EVPPKey getKey() const;
/// Return key as openssl EVP_PKEY wrapper object.
EVPPKey getKey() const;
/// Return key as openssl EVP_PKEY wrapper object.
bool hasX509Certificate() const;
/// Returns true if container has X509 certificate.
bool hasX509Certificate() const;
/// Returns true if container has X509 certificate.
const X509Certificate& getX509Certificate() const;
/// Returns the X509 certificate.
/// Throws NotFoundException if there is no certificate.
const X509Certificate & getX509Certificate() const;
/// Returns the X509 certificate.
/// Throws NotFoundException if there is no certificate.
const CAList& getCACerts() const;
/// Returns the list of CA certificates in this container.
const CAList & getCACerts() const;
/// Returns the list of CA certificates in this container.
const std::string& getFriendlyName() const;
/// Returns the friendly name of the certificate bag.
const std::string & getFriendlyName() const;
/// Returns the friendly name of the certificate bag.
const CANameList& getFriendlyNamesCA() const;
/// Returns a list of CA certificates friendly names.
const CANameList & getFriendlyNamesCA() const;
/// Returns a list of CA certificates friendly names.
private:
void load(PKCS12* pPKCS12, const std::string& password = "");
std::string extractFriendlyName(X509* pCert);
private:
void load(PKCS12 * pPKCS12, const std::string & password = "");
std::string extractFriendlyName(X509 * pCert);
#ifdef POCO_ENABLE_CPP11
typedef std::unique_ptr<X509Certificate> CertPtr;
typedef std::unique_ptr<X509Certificate> CertPtr;
#else
typedef std::auto_ptr<X509Certificate> CertPtr;
typedef std::auto_ptr<X509Certificate> CertPtr;
#endif // #ifdef POCO_ENABLE_CPP11
OpenSSLInitializer _openSSLInitializer;
EVP_PKEY* _pKey;
CertPtr _pX509Cert;
CAList _caCertList;
CANameList _caCertNames;
std::string _pkcsFriendlyName;
};
OpenSSLInitializer _openSSLInitializer;
EVP_PKEY * _pKey;
CertPtr _pX509Cert;
CAList _caCertList;
CANameList _caCertNames;
std::string _pkcsFriendlyName;
};
//
// inlines
//
//
// inlines
//
inline bool PKCS12Container::hasX509Certificate() const
{
return _pX509Cert.get() != 0;
}
inline const X509Certificate & PKCS12Container::getX509Certificate() const
{
if (!hasX509Certificate())
throw NotFoundException("PKCS12Container X509 certificate");
return *_pX509Cert;
}
inline const std::string & PKCS12Container::getFriendlyName() const
{
return _pkcsFriendlyName;
}
inline const PKCS12Container::CAList & PKCS12Container::getCACerts() const
{
return _caCertList;
}
inline const PKCS12Container::CANameList & PKCS12Container::getFriendlyNamesCA() const
{
return _caCertNames;
}
inline bool PKCS12Container::hasKey() const
{
return _pKey != 0;
}
inline EVPPKey PKCS12Container::getKey() const
{
return EVPPKey(_pKey);
}
inline bool PKCS12Container::hasX509Certificate() const
{
return _pX509Cert.get() != 0;
}
inline const X509Certificate& PKCS12Container::getX509Certificate() const
{
if (!hasX509Certificate())
throw NotFoundException("PKCS12Container X509 certificate");
return *_pX509Cert;
}
inline const std::string& PKCS12Container::getFriendlyName() const
{
return _pkcsFriendlyName;
}
inline const PKCS12Container::CAList& PKCS12Container::getCACerts() const
{
return _caCertList;
}
inline const PKCS12Container::CANameList& PKCS12Container::getFriendlyNamesCA() const
{
return _caCertNames;
}
inline bool PKCS12Container::hasKey() const
{
return _pKey != 0;
}
inline EVPPKey PKCS12Container::getKey() const
{
return EVPPKey(_pKey);
}
} } // namespace Poco::Crypto
} // namespace Poco::Crypto
#endif // Crypto_PKCS12Container_INCLUDED

View File

@ -18,60 +18,63 @@
#define Crypto_RSACipherImpl_INCLUDED
#include "Poco/Crypto/Crypto.h"
#include "Poco/Crypto/Cipher.h"
#include "Poco/Crypto/RSAKey.h"
#include "Poco/Crypto/OpenSSLInitializer.h"
#include <openssl/evp.h>
#include "Poco/Crypto/Cipher.h"
#include "Poco/Crypto/Crypto.h"
#include "Poco/Crypto/OpenSSLInitializer.h"
#include "Poco/Crypto/RSAKey.h"
namespace Poco {
namespace Crypto {
class RSACipherImpl: public Cipher
/// An implementation of the Cipher class for
/// asymmetric (public-private key) encryption
/// based on the the RSA algorithm in OpenSSL's
/// crypto library.
///
/// Encryption is using the public key, decryption
/// requires the private key.
namespace Poco
{
public:
RSACipherImpl(const RSAKey& key, RSAPaddingMode paddingMode);
/// Creates a new RSACipherImpl object for the given RSAKey
/// and using the given padding mode.
virtual ~RSACipherImpl();
/// Destroys the RSACipherImpl.
const std::string& name() const;
/// Returns the name of the Cipher.
CryptoTransform* createEncryptor();
/// Creates an encryptor object.
CryptoTransform* createDecryptor();
/// Creates a decryptor object.
private:
RSAKey _key;
RSAPaddingMode _paddingMode;
OpenSSLInitializer _openSSLInitializer;
};
//
// Inlines
//
inline const std::string& RSACipherImpl::name() const
namespace Crypto
{
return _key.name();
class RSACipherImpl : public Cipher
/// An implementation of the Cipher class for
/// asymmetric (public-private key) encryption
/// based on the the RSA algorithm in OpenSSL's
/// crypto library.
///
/// Encryption is using the public key, decryption
/// requires the private key.
{
public:
RSACipherImpl(const RSAKey & key, RSAPaddingMode paddingMode);
/// Creates a new RSACipherImpl object for the given RSAKey
/// and using the given padding mode.
virtual ~RSACipherImpl();
/// Destroys the RSACipherImpl.
const std::string & name() const;
/// Returns the name of the Cipher.
CryptoTransform * createEncryptor();
/// Creates an encryptor object.
CryptoTransform * createDecryptor();
/// Creates a decryptor object.
private:
RSAKey _key;
RSAPaddingMode _paddingMode;
OpenSSLInitializer _openSSLInitializer;
};
//
// Inlines
//
inline const std::string & RSACipherImpl::name() const
{
return _key.name();
}
}
} } // namespace Poco::Crypto
} // namespace Poco::Crypto
#endif // Crypto_RSACipherImpl_INCLUDED

View File

@ -18,94 +18,97 @@
#define Crypto_RSADigestEngine_INCLUDED
#include "Poco/Crypto/Crypto.h"
#include "Poco/Crypto/RSAKey.h"
#include "Poco/DigestEngine.h"
#include "Poco/Crypto/DigestEngine.h"
#include <istream>
#include <ostream>
#include "Poco/Crypto/Crypto.h"
#include "Poco/Crypto/DigestEngine.h"
#include "Poco/Crypto/RSAKey.h"
#include "Poco/DigestEngine.h"
namespace Poco {
namespace Crypto {
class Crypto_API RSADigestEngine: public Poco::DigestEngine
/// This class implements a Poco::DigestEngine that can be
/// used to compute a secure digital signature.
///
/// First another Poco::Crypto::DigestEngine is created and
/// used to compute a cryptographic hash of the data to be
/// signed. Then, the hash value is encrypted, using
/// the RSA private key.
///
/// To verify a signature, pass it to the verify()
/// member function. It will decrypt the signature
/// using the RSA public key and compare the resulting
/// hash with the actual hash of the data.
namespace Poco
{
namespace Crypto
{
public:
enum DigestType
{
DIGEST_MD5,
DIGEST_SHA1
};
//@ deprecated
RSADigestEngine(const RSAKey& key, DigestType digestType = DIGEST_SHA1);
/// Creates the RSADigestEngine with the given RSA key,
/// using the MD5 or SHA-1 hash algorithm.
/// Kept for backward compatibility
RSADigestEngine(const RSAKey& key, const std::string &name);
/// Creates the RSADigestEngine with the given RSA key,
/// using the hash algorithm with the given name
/// (e.g., "MD5", "SHA1", "SHA256", "SHA512", etc.).
/// See the OpenSSL documentation for a list of supported digest algorithms.
///
/// Throws a Poco::NotFoundException if no algorithm with the given name exists.
~RSADigestEngine();
/// Destroys the RSADigestEngine.
std::size_t digestLength() const;
/// Returns the length of the digest in bytes.
void reset();
/// Resets the engine so that a new
/// digest can be computed.
const DigestEngine::Digest& digest();
/// Finishes the computation of the digest
/// (the first time it's called) and
/// returns the message digest.
///
/// Can be called multiple times.
const DigestEngine::Digest& signature();
/// Signs the digest using the RSA algorithm
/// and the private key (the first time it's
/// called) and returns the result.
///
/// Can be called multiple times.
bool verify(const DigestEngine::Digest& signature);
/// Verifies the data against the signature.
///
/// Returns true if the signature can be verified, false otherwise.
protected:
void updateImpl(const void* data, std::size_t length);
private:
RSAKey _key;
Poco::Crypto::DigestEngine _engine;
Poco::DigestEngine::Digest _digest;
Poco::DigestEngine::Digest _signature;
};
} } // namespace Poco::Crypto
class Crypto_API RSADigestEngine : public Poco::DigestEngine
/// This class implements a Poco::DigestEngine that can be
/// used to compute a secure digital signature.
///
/// First another Poco::Crypto::DigestEngine is created and
/// used to compute a cryptographic hash of the data to be
/// signed. Then, the hash value is encrypted, using
/// the RSA private key.
///
/// To verify a signature, pass it to the verify()
/// member function. It will decrypt the signature
/// using the RSA public key and compare the resulting
/// hash with the actual hash of the data.
{
public:
enum DigestType
{
DIGEST_MD5,
DIGEST_SHA1
};
//@ deprecated
RSADigestEngine(const RSAKey & key, DigestType digestType = DIGEST_SHA1);
/// Creates the RSADigestEngine with the given RSA key,
/// using the MD5 or SHA-1 hash algorithm.
/// Kept for backward compatibility
RSADigestEngine(const RSAKey & key, const std::string & name);
/// Creates the RSADigestEngine with the given RSA key,
/// using the hash algorithm with the given name
/// (e.g., "MD5", "SHA1", "SHA256", "SHA512", etc.).
/// See the OpenSSL documentation for a list of supported digest algorithms.
///
/// Throws a Poco::NotFoundException if no algorithm with the given name exists.
~RSADigestEngine();
/// Destroys the RSADigestEngine.
std::size_t digestLength() const;
/// Returns the length of the digest in bytes.
void reset();
/// Resets the engine so that a new
/// digest can be computed.
const DigestEngine::Digest & digest();
/// Finishes the computation of the digest
/// (the first time it's called) and
/// returns the message digest.
///
/// Can be called multiple times.
const DigestEngine::Digest & signature();
/// Signs the digest using the RSA algorithm
/// and the private key (the first time it's
/// called) and returns the result.
///
/// Can be called multiple times.
bool verify(const DigestEngine::Digest & signature);
/// Verifies the data against the signature.
///
/// Returns true if the signature can be verified, false otherwise.
protected:
void updateImpl(const void * data, std::size_t length);
private:
RSAKey _key;
Poco::Crypto::DigestEngine _engine;
Poco::DigestEngine::Digest _digest;
Poco::DigestEngine::Digest _signature;
};
}
} // namespace Poco::Crypto
#endif // Crypto_RSADigestEngine_INCLUDED

View File

@ -23,103 +23,102 @@
#include "Poco/Crypto/RSAKeyImpl.h"
namespace Poco {
namespace Crypto {
class X509Certificate;
class PKCS12Container;
class Crypto_API RSAKey : public KeyPair
/// This class stores an RSA key pair, consisting
/// of private and public key. Storage of the private
/// key is optional.
///
/// If a private key is available, the RSAKey can be
/// used for decrypting data (encrypted with the public key)
/// or computing secure digital signatures.
namespace Poco
{
public:
enum KeyLength
{
KL_512 = 512,
KL_1024 = 1024,
KL_2048 = 2048,
KL_4096 = 4096
};
enum Exponent
{
EXP_SMALL = 0,
EXP_LARGE
};
RSAKey(const EVPPKey& key);
/// Constructs ECKeyImpl by extracting the EC key.
RSAKey(const X509Certificate& cert);
/// Extracts the RSA public key from the given certificate.
RSAKey(const PKCS12Container& cert);
/// Extracts the RSA private key from the given certificate.
RSAKey(KeyLength keyLength, Exponent exp);
/// Creates the RSAKey. Creates a new public/private keypair using the given parameters.
/// Can be used to sign data and verify signatures.
RSAKey(const std::string& publicKeyFile,
const std::string& privateKeyFile = "",
const std::string& privateKeyPassphrase = "");
/// Creates the RSAKey, by reading public and private key from the given files and
/// using the given passphrase for the private key.
///
/// Cannot be used for signing or decryption unless a private key is available.
///
/// If a private key is specified, you don't need to specify a public key file.
/// OpenSSL will auto-create the public key from the private key.
RSAKey(std::istream* pPublicKeyStream,
std::istream* pPrivateKeyStream = 0,
const std::string& privateKeyPassphrase = "");
/// Creates the RSAKey, by reading public and private key from the given streams and
/// using the given passphrase for the private key.
///
/// Cannot be used for signing or decryption unless a private key is available.
///
/// If a private key is specified, you don't need to specify a public key file.
/// OpenSSL will auto-create the public key from the private key.
~RSAKey();
/// Destroys the RSAKey.
RSAKeyImpl::ByteVec modulus() const;
/// Returns the RSA modulus.
RSAKeyImpl::ByteVec encryptionExponent() const;
/// Returns the RSA encryption exponent.
RSAKeyImpl::ByteVec decryptionExponent() const;
/// Returns the RSA decryption exponent.
RSAKeyImpl::Ptr impl() const;
/// Returns the impl object.
private:
RSAKeyImpl::Ptr _pImpl;
};
//
// inlines
//
inline RSAKeyImpl::Ptr RSAKey::impl() const
namespace Crypto
{
return _pImpl;
class X509Certificate;
class PKCS12Container;
class Crypto_API RSAKey : public KeyPair
/// This class stores an RSA key pair, consisting
/// of private and public key. Storage of the private
/// key is optional.
///
/// If a private key is available, the RSAKey can be
/// used for decrypting data (encrypted with the public key)
/// or computing secure digital signatures.
{
public:
enum KeyLength
{
KL_512 = 512,
KL_1024 = 1024,
KL_2048 = 2048,
KL_4096 = 4096
};
enum Exponent
{
EXP_SMALL = 0,
EXP_LARGE
};
RSAKey(const EVPPKey & key);
/// Constructs ECKeyImpl by extracting the EC key.
RSAKey(const X509Certificate & cert);
/// Extracts the RSA public key from the given certificate.
RSAKey(const PKCS12Container & cert);
/// Extracts the RSA private key from the given certificate.
RSAKey(KeyLength keyLength, Exponent exp);
/// Creates the RSAKey. Creates a new public/private keypair using the given parameters.
/// Can be used to sign data and verify signatures.
RSAKey(const std::string & publicKeyFile, const std::string & privateKeyFile = "", const std::string & privateKeyPassphrase = "");
/// Creates the RSAKey, by reading public and private key from the given files and
/// using the given passphrase for the private key.
///
/// Cannot be used for signing or decryption unless a private key is available.
///
/// If a private key is specified, you don't need to specify a public key file.
/// OpenSSL will auto-create the public key from the private key.
RSAKey(std::istream * pPublicKeyStream, std::istream * pPrivateKeyStream = 0, const std::string & privateKeyPassphrase = "");
/// Creates the RSAKey, by reading public and private key from the given streams and
/// using the given passphrase for the private key.
///
/// Cannot be used for signing or decryption unless a private key is available.
///
/// If a private key is specified, you don't need to specify a public key file.
/// OpenSSL will auto-create the public key from the private key.
~RSAKey();
/// Destroys the RSAKey.
RSAKeyImpl::ByteVec modulus() const;
/// Returns the RSA modulus.
RSAKeyImpl::ByteVec encryptionExponent() const;
/// Returns the RSA encryption exponent.
RSAKeyImpl::ByteVec decryptionExponent() const;
/// Returns the RSA decryption exponent.
RSAKeyImpl::Ptr impl() const;
/// Returns the impl object.
private:
RSAKeyImpl::Ptr _pImpl;
};
//
// inlines
//
inline RSAKeyImpl::Ptr RSAKey::impl() const
{
return _pImpl;
}
}
} } // namespace Poco::Crypto
} // namespace Poco::Crypto
#endif // Crypto_RSAKey_INCLUDED

View File

@ -18,15 +18,15 @@
#define Crypto_RSAKeyImplImpl_INCLUDED
#include <istream>
#include <ostream>
#include <vector>
#include "Poco/AutoPtr.h"
#include "Poco/Crypto/Crypto.h"
#include "Poco/Crypto/EVPPKey.h"
#include "Poco/Crypto/KeyPairImpl.h"
#include "Poco/Crypto/OpenSSLInitializer.h"
#include "Poco/RefCountedObject.h"
#include "Poco/AutoPtr.h"
#include <istream>
#include <ostream>
#include <vector>
struct bignum_st;
@ -35,107 +35,108 @@ typedef struct bignum_st BIGNUM;
typedef struct rsa_st RSA;
namespace Poco {
namespace Crypto {
class X509Certificate;
class PKCS12Container;
class RSAKeyImpl: public KeyPairImpl
/// class RSAKeyImpl
namespace Poco
{
public:
typedef Poco::AutoPtr<RSAKeyImpl> Ptr;
typedef std::vector<unsigned char> ByteVec;
RSAKeyImpl(const EVPPKey& key);
/// Constructs ECKeyImpl by extracting the EC key.
RSAKeyImpl(const X509Certificate& cert);
/// Extracts the RSA public key from the given certificate.
RSAKeyImpl(const PKCS12Container& cert);
/// Extracts the EC private key from the given certificate.
RSAKeyImpl(int keyLength, unsigned long exponent);
/// Creates the RSAKey. Creates a new public/private keypair using the given parameters.
/// Can be used to sign data and verify signatures.
RSAKeyImpl(const std::string& publicKeyFile, const std::string& privateKeyFile, const std::string& privateKeyPassphrase);
/// Creates the RSAKey, by reading public and private key from the given files and
/// using the given passphrase for the private key. Can only by used for signing if
/// a private key is available.
RSAKeyImpl(std::istream* pPublicKeyStream, std::istream* pPrivateKeyStream, const std::string& privateKeyPassphrase);
/// Creates the RSAKey. Can only by used for signing if pPrivKey
/// is not null. If a private key file is specified, you don't need to
/// specify a public key file. OpenSSL will auto-create it from the private key.
~RSAKeyImpl();
/// Destroys the RSAKeyImpl.
RSA* getRSA();
/// Returns the OpenSSL RSA object.
const RSA* getRSA() const;
/// Returns the OpenSSL RSA object.
int size() const;
/// Returns the RSA modulus size.
ByteVec modulus() const;
/// Returns the RSA modulus.
ByteVec encryptionExponent() const;
/// Returns the RSA encryption exponent.
ByteVec decryptionExponent() const;
/// Returns the RSA decryption exponent.
void save(const std::string& publicKeyFile,
const std::string& privateKeyFile = "",
const std::string& privateKeyPassphrase = "") const;
/// Exports the public and private keys to the given files.
///
/// If an empty filename is specified, the corresponding key
/// is not exported.
void save(std::ostream* pPublicKeyStream,
std::ostream* pPrivateKeyStream = 0,
const std::string& privateKeyPassphrase = "") const;
/// Exports the public and private key to the given streams.
///
/// If a null pointer is passed for a stream, the corresponding
/// key is not exported.
private:
RSAKeyImpl();
void freeRSA();
static ByteVec convertToByteVec(const BIGNUM* bn);
RSA* _pRSA;
};
//
// inlines
//
inline RSA* RSAKeyImpl::getRSA()
namespace Crypto
{
return _pRSA;
class X509Certificate;
class PKCS12Container;
class RSAKeyImpl : public KeyPairImpl
/// class RSAKeyImpl
{
public:
typedef Poco::AutoPtr<RSAKeyImpl> Ptr;
typedef std::vector<unsigned char> ByteVec;
RSAKeyImpl(const EVPPKey & key);
/// Constructs ECKeyImpl by extracting the EC key.
RSAKeyImpl(const X509Certificate & cert);
/// Extracts the RSA public key from the given certificate.
RSAKeyImpl(const PKCS12Container & cert);
/// Extracts the EC private key from the given certificate.
RSAKeyImpl(int keyLength, unsigned long exponent);
/// Creates the RSAKey. Creates a new public/private keypair using the given parameters.
/// Can be used to sign data and verify signatures.
RSAKeyImpl(const std::string & publicKeyFile, const std::string & privateKeyFile, const std::string & privateKeyPassphrase);
/// Creates the RSAKey, by reading public and private key from the given files and
/// using the given passphrase for the private key. Can only by used for signing if
/// a private key is available.
RSAKeyImpl(std::istream * pPublicKeyStream, std::istream * pPrivateKeyStream, const std::string & privateKeyPassphrase);
/// Creates the RSAKey. Can only by used for signing if pPrivKey
/// is not null. If a private key file is specified, you don't need to
/// specify a public key file. OpenSSL will auto-create it from the private key.
~RSAKeyImpl();
/// Destroys the RSAKeyImpl.
RSA * getRSA();
/// Returns the OpenSSL RSA object.
const RSA * getRSA() const;
/// Returns the OpenSSL RSA object.
int size() const;
/// Returns the RSA modulus size.
ByteVec modulus() const;
/// Returns the RSA modulus.
ByteVec encryptionExponent() const;
/// Returns the RSA encryption exponent.
ByteVec decryptionExponent() const;
/// Returns the RSA decryption exponent.
void save(const std::string & publicKeyFile, const std::string & privateKeyFile = "", const std::string & privateKeyPassphrase = "")
const;
/// Exports the public and private keys to the given files.
///
/// If an empty filename is specified, the corresponding key
/// is not exported.
void
save(std::ostream * pPublicKeyStream, std::ostream * pPrivateKeyStream = 0, const std::string & privateKeyPassphrase = "") const;
/// Exports the public and private key to the given streams.
///
/// If a null pointer is passed for a stream, the corresponding
/// key is not exported.
private:
RSAKeyImpl();
void freeRSA();
static ByteVec convertToByteVec(const BIGNUM * bn);
RSA * _pRSA;
};
//
// inlines
//
inline RSA * RSAKeyImpl::getRSA()
{
return _pRSA;
}
inline const RSA * RSAKeyImpl::getRSA() const
{
return _pRSA;
}
}
inline const RSA* RSAKeyImpl::getRSA() const
{
return _pRSA;
}
} } // namespace Poco::Crypto
} // namespace Poco::Crypto
#endif // Crypto_RSAKeyImplImpl_INCLUDED

View File

@ -18,228 +18,231 @@
#define Crypto_X509Certificate_INCLUDED
#include <istream>
#include <set>
#include <vector>
#include <openssl/ssl.h>
#include "Poco/Crypto/Crypto.h"
#include "Poco/Crypto/OpenSSLInitializer.h"
#include "Poco/DateTime.h"
#include "Poco/SharedPtr.h"
#include <vector>
#include <set>
#include <istream>
#include <openssl/ssl.h>
namespace Poco {
namespace Crypto {
class Crypto_API X509Certificate
/// This class represents a X509 Certificate.
namespace Poco
{
public:
typedef std::vector<X509Certificate> List;
enum NID
/// Name identifier for extracting information from
/// a certificate subject's or issuer's distinguished name.
{
NID_COMMON_NAME = 13,
NID_COUNTRY = 14,
NID_LOCALITY_NAME = 15,
NID_STATE_OR_PROVINCE = 16,
NID_ORGANIZATION_NAME = 17,
NID_ORGANIZATION_UNIT_NAME = 18,
NID_PKCS9_EMAIL_ADDRESS = 48,
NID_SERIAL_NUMBER = 105
};
explicit X509Certificate(std::istream& istr);
/// Creates the X509Certificate object by reading
/// a certificate in PEM format from a stream.
explicit X509Certificate(const std::string& path);
/// Creates the X509Certificate object by reading
/// a certificate in PEM format from a file.
explicit X509Certificate(X509* pCert);
/// Creates the X509Certificate from an existing
/// OpenSSL certificate. Ownership is taken of
/// the certificate.
X509Certificate(X509* pCert, bool shared);
/// Creates the X509Certificate from an existing
/// OpenSSL certificate. Ownership is taken of
/// the certificate. If shared is true, the
/// certificate's reference count is incremented.
X509Certificate(const X509Certificate& cert);
/// Creates the certificate by copying another one.
X509Certificate& operator = (const X509Certificate& cert);
/// Assigns a certificate.
void swap(X509Certificate& cert);
/// Exchanges the certificate with another one.
~X509Certificate();
/// Destroys the X509Certificate.
long version() const;
/// Returns the version of the certificate.
const std::string& serialNumber() const;
/// Returns the certificate serial number as a
/// string in decimal encoding.
const std::string& issuerName() const;
/// Returns the certificate issuer's distinguished name.
std::string issuerName(NID nid) const;
/// Extracts the information specified by the given
/// NID (name identifier) from the certificate issuer's
/// distinguished name.
const std::string& subjectName() const;
/// Returns the certificate subject's distinguished name.
std::string subjectName(NID nid) const;
/// Extracts the information specified by the given
/// NID (name identifier) from the certificate subject's
/// distinguished name.
std::string commonName() const;
/// Returns the common name stored in the certificate
/// subject's distinguished name.
void extractNames(std::string& commonName, std::set<std::string>& domainNames) const;
/// Extracts the common name and the alias domain names from the
/// certificate.
Poco::DateTime validFrom() const;
/// Returns the date and time the certificate is valid from.
Poco::DateTime expiresOn() const;
/// Returns the date and time the certificate expires.
void save(std::ostream& stream) const;
/// Writes the certificate to the given stream.
/// The certificate is written in PEM format.
void save(const std::string& path) const;
/// Writes the certificate to the file given by path.
/// The certificate is written in PEM format.
bool issuedBy(const X509Certificate& issuerCertificate) const;
/// Checks whether the certificate has been issued by
/// the issuer given by issuerCertificate. This can be
/// used to validate a certificate chain.
///
/// Verifies if the certificate has been signed with the
/// issuer's private key, using the public key from the issuer
/// certificate.
///
/// Returns true if verification against the issuer certificate
/// was successful, false otherwise.
bool equals(const X509Certificate& otherCertificate) const;
/// Checks whether the certificate is equal to
/// the other certificate, by comparing the hashes
/// of both certificates.
///
/// Returns true if both certificates are identical,
/// otherwise false.
const X509* certificate() const;
/// Returns the underlying OpenSSL certificate.
X509* dup() const;
/// Duplicates and returns the underlying OpenSSL certificate. Note that
/// the caller assumes responsibility for the lifecycle of the created
/// certificate.
std::string signatureAlgorithm() const;
/// Returns the certificate signature algorithm long name.
void print(std::ostream& out) const;
/// Prints the certificate information to ostream.
static List readPEM(const std::string& pemFileName);
/// Reads and returns a list of certificates from
/// the specified PEM file.
static void writePEM(const std::string& pemFileName, const List& list);
/// Writes the list of certificates to the specified PEM file.
protected:
void load(std::istream& stream);
/// Loads the certificate from the given stream. The
/// certificate must be in PEM format.
void load(const std::string& path);
/// Loads the certificate from the given file. The
/// certificate must be in PEM format.
void init();
/// Extracts issuer and subject name from the certificate.
private:
enum
{
NAME_BUFFER_SIZE = 256
};
std::string _issuerName;
std::string _subjectName;
std::string _serialNumber;
X509* _pCert;
OpenSSLInitializer _openSSLInitializer;
};
//
// inlines
//
inline long X509Certificate::version() const
namespace Crypto
{
// This is defined by standards (X.509 et al) to be
// one less than the certificate version.
// So, eg. a version 3 certificate will return 2.
return X509_get_version(_pCert) + 1;
class Crypto_API X509Certificate
/// This class represents a X509 Certificate.
{
public:
typedef std::vector<X509Certificate> List;
enum NID
/// Name identifier for extracting information from
/// a certificate subject's or issuer's distinguished name.
{
NID_COMMON_NAME = 13,
NID_COUNTRY = 14,
NID_LOCALITY_NAME = 15,
NID_STATE_OR_PROVINCE = 16,
NID_ORGANIZATION_NAME = 17,
NID_ORGANIZATION_UNIT_NAME = 18,
NID_PKCS9_EMAIL_ADDRESS = 48,
NID_SERIAL_NUMBER = 105
};
explicit X509Certificate(std::istream & istr);
/// Creates the X509Certificate object by reading
/// a certificate in PEM format from a stream.
explicit X509Certificate(const std::string & path);
/// Creates the X509Certificate object by reading
/// a certificate in PEM format from a file.
explicit X509Certificate(X509 * pCert);
/// Creates the X509Certificate from an existing
/// OpenSSL certificate. Ownership is taken of
/// the certificate.
X509Certificate(X509 * pCert, bool shared);
/// Creates the X509Certificate from an existing
/// OpenSSL certificate. Ownership is taken of
/// the certificate. If shared is true, the
/// certificate's reference count is incremented.
X509Certificate(const X509Certificate & cert);
/// Creates the certificate by copying another one.
X509Certificate & operator=(const X509Certificate & cert);
/// Assigns a certificate.
void swap(X509Certificate & cert);
/// Exchanges the certificate with another one.
~X509Certificate();
/// Destroys the X509Certificate.
long version() const;
/// Returns the version of the certificate.
const std::string & serialNumber() const;
/// Returns the certificate serial number as a
/// string in decimal encoding.
const std::string & issuerName() const;
/// Returns the certificate issuer's distinguished name.
std::string issuerName(NID nid) const;
/// Extracts the information specified by the given
/// NID (name identifier) from the certificate issuer's
/// distinguished name.
const std::string & subjectName() const;
/// Returns the certificate subject's distinguished name.
std::string subjectName(NID nid) const;
/// Extracts the information specified by the given
/// NID (name identifier) from the certificate subject's
/// distinguished name.
std::string commonName() const;
/// Returns the common name stored in the certificate
/// subject's distinguished name.
void extractNames(std::string & commonName, std::set<std::string> & domainNames) const;
/// Extracts the common name and the alias domain names from the
/// certificate.
Poco::DateTime validFrom() const;
/// Returns the date and time the certificate is valid from.
Poco::DateTime expiresOn() const;
/// Returns the date and time the certificate expires.
void save(std::ostream & stream) const;
/// Writes the certificate to the given stream.
/// The certificate is written in PEM format.
void save(const std::string & path) const;
/// Writes the certificate to the file given by path.
/// The certificate is written in PEM format.
bool issuedBy(const X509Certificate & issuerCertificate) const;
/// Checks whether the certificate has been issued by
/// the issuer given by issuerCertificate. This can be
/// used to validate a certificate chain.
///
/// Verifies if the certificate has been signed with the
/// issuer's private key, using the public key from the issuer
/// certificate.
///
/// Returns true if verification against the issuer certificate
/// was successful, false otherwise.
bool equals(const X509Certificate & otherCertificate) const;
/// Checks whether the certificate is equal to
/// the other certificate, by comparing the hashes
/// of both certificates.
///
/// Returns true if both certificates are identical,
/// otherwise false.
const X509 * certificate() const;
/// Returns the underlying OpenSSL certificate.
X509 * dup() const;
/// Duplicates and returns the underlying OpenSSL certificate. Note that
/// the caller assumes responsibility for the lifecycle of the created
/// certificate.
std::string signatureAlgorithm() const;
/// Returns the certificate signature algorithm long name.
void print(std::ostream & out) const;
/// Prints the certificate information to ostream.
static List readPEM(const std::string & pemFileName);
/// Reads and returns a list of certificates from
/// the specified PEM file.
static void writePEM(const std::string & pemFileName, const List & list);
/// Writes the list of certificates to the specified PEM file.
protected:
void load(std::istream & stream);
/// Loads the certificate from the given stream. The
/// certificate must be in PEM format.
void load(const std::string & path);
/// Loads the certificate from the given file. The
/// certificate must be in PEM format.
void init();
/// Extracts issuer and subject name from the certificate.
private:
enum
{
NAME_BUFFER_SIZE = 256
};
std::string _issuerName;
std::string _subjectName;
std::string _serialNumber;
X509 * _pCert;
OpenSSLInitializer _openSSLInitializer;
};
//
// inlines
//
inline long X509Certificate::version() const
{
// This is defined by standards (X.509 et al) to be
// one less than the certificate version.
// So, eg. a version 3 certificate will return 2.
return X509_get_version(_pCert) + 1;
}
inline const std::string & X509Certificate::serialNumber() const
{
return _serialNumber;
}
inline const std::string & X509Certificate::issuerName() const
{
return _issuerName;
}
inline const std::string & X509Certificate::subjectName() const
{
return _subjectName;
}
inline const X509 * X509Certificate::certificate() const
{
return _pCert;
}
inline X509 * X509Certificate::dup() const
{
return X509_dup(_pCert);
}
}
inline const std::string& X509Certificate::serialNumber() const
{
return _serialNumber;
}
inline const std::string& X509Certificate::issuerName() const
{
return _issuerName;
}
inline const std::string& X509Certificate::subjectName() const
{
return _subjectName;
}
inline const X509* X509Certificate::certificate() const
{
return _pCert;
}
inline X509* X509Certificate::dup() const
{
return X509_dup(_pCert);
}
} } // namespace Poco::Crypto
} // namespace Poco::Crypto
#endif // Crypto_X509Certificate_INCLUDED

View File

@ -22,36 +22,12 @@
#if OPENSSL_VERSION_NUMBER >= 0x0907000L
#include <openssl/conf.h>
#endif
#if defined(POCO_OS_FAMILY_WINDOWS)
#define POCO_STR_HELPER(x) #x
#define POCO_STR(x) POCO_STR_HELPER(x)
#if defined POCO_INTERNAL_OPENSSL_MSVC_VER
#define POCO_INTERNAL_OPENSSL_BUILD \
" (POCO internal build, MSVC version " \
POCO_STR(POCO_INTERNAL_OPENSSL_MSVC_VER) ")"
#else
#define POCO_INTERNAL_OPENSSL_BUILD ""
#endif
#pragma message (OPENSSL_VERSION_TEXT POCO_INTERNAL_OPENSSL_BUILD)
#endif
using Poco::RandomInputStream;
using Poco::Thread;
#if defined(_MSC_VER) && !defined(_DLL) && defined(POCO_INTERNAL_OPENSSL_MSVC_VER)
#if (POCO_MSVS_VERSION >= 2015)
FILE _iob[] = { *stdin, *stdout, *stderr };
extern "C" FILE * __cdecl __iob_func(void) { return _iob; }
#endif // (POCO_MSVS_VERSION >= 2015)
#if (POCO_MSVS_VERSION < 2012)
extern "C" __declspec(noreturn) void __cdecl __report_rangecheckfailure(void) { ::ExitProcess(1); }
#endif // (POCO_MSVS_VERSION < 2012)
#endif // _MSC_VER && _MT && !POCO_EXTERNAL_OPENSSL && (POCO_MSVS_VERSION < 2013)
namespace Poco {
@ -100,7 +76,6 @@ void OpenSSLInitializer::initialize()
int nMutexes = CRYPTO_num_locks();
_mutexes = new Poco::FastMutex[nMutexes];
CRYPTO_set_locking_callback(&OpenSSLInitializer::lock);
#ifndef POCO_OS_FAMILY_WINDOWS
// Not needed on Windows (see SF #110: random unhandled exceptions when linking with ssl).
// https://sourceforge.net/p/poco/bugs/110/
//
@ -109,7 +84,6 @@ void OpenSSLInitializer::initialize()
// then a default implementation is used - on Windows and BeOS this uses the system's
// default thread identifying APIs"
CRYPTO_set_id_callback(&OpenSSLInitializer::id);
#endif
CRYPTO_set_dynlock_create_callback(&OpenSSLInitializer::dynlockCreate);
CRYPTO_set_dynlock_lock_callback(&OpenSSLInitializer::dynlock);
CRYPTO_set_dynlock_destroy_callback(&OpenSSLInitializer::dynlockDestroy);
@ -124,9 +98,7 @@ void OpenSSLInitializer::uninitialize()
EVP_cleanup();
ERR_free_strings();
CRYPTO_set_locking_callback(0);
#ifndef POCO_OS_FAMILY_WINDOWS
CRYPTO_set_id_callback(0);
#endif
delete [] _mutexes;
CONF_modules_free();

View File

@ -102,7 +102,7 @@ RSAKeyImpl::RSAKeyImpl(const std::string& publicKeyFile,
{
BIO* bio = BIO_new(BIO_s_file());
if (!bio) throw Poco::IOException("Cannot create BIO for reading public key", publicKeyFile);
int rc = BIO_read_filename(bio, publicKeyFile.c_str());
int rc = BIO_read_filename(bio, const_cast<char *>(publicKeyFile.c_str()));
if (rc)
{
RSA* pubKey = PEM_read_bio_RSAPublicKey(bio, &_pRSA, 0, 0);
@ -132,7 +132,7 @@ RSAKeyImpl::RSAKeyImpl(const std::string& publicKeyFile,
{
BIO* bio = BIO_new(BIO_s_file());
if (!bio) throw Poco::IOException("Cannot create BIO for reading private key", privateKeyFile);
int rc = BIO_read_filename(bio, privateKeyFile.c_str());
int rc = BIO_read_filename(bio, const_cast<char *>(privateKeyFile.c_str()));
if (rc)
{
RSA* privKey = 0;

View File

@ -20,10 +20,6 @@
#include "Poco/Format.h"
#include <sstream>
#include <openssl/pem.h>
#ifdef _WIN32
// fix for WIN32 header conflict
#undef X509_NAME
#endif
#include <openssl/x509v3.h>
#include <openssl/err.h>
#include <openssl/evp.h>
@ -133,7 +129,7 @@ void X509Certificate::load(const std::string& path)
BIO *pBIO = BIO_new(BIO_s_file());
if (!pBIO) throw Poco::IOException("Cannot create BIO for reading certificate file", path);
if (!BIO_read_filename(pBIO, path.c_str()))
if (!BIO_read_filename(pBIO, const_cast<char *>(path.c_str())))
{
BIO_free(pBIO);
throw Poco::OpenFileException("Cannot open certificate file for reading", path);

File diff suppressed because it is too large Load Diff

View File

@ -18,79 +18,81 @@
#define Data_ODBC_ConnectionHandle_INCLUDED
#include "Poco/Data/ODBC/ODBC.h"
#include "Poco/Data/ODBC/EnvironmentHandle.h"
#ifdef POCO_OS_FAMILY_WINDOWS
#include <windows.h>
#endif
#include "Poco/Data/ODBC/ODBC.h"
#include <sqltypes.h>
namespace Poco {
namespace Data {
namespace ODBC {
class ODBC_API ConnectionHandle
/// ODBC connection handle class
namespace Poco
{
public:
ConnectionHandle(EnvironmentHandle* pEnvironment = 0);
/// Creates the ConnectionHandle.
~ConnectionHandle();
/// Creates the ConnectionHandle.
operator const SQLHDBC& () const;
/// Const conversion operator into reference to native type.
const SQLHDBC& handle() const;
/// Returns const reference to handle;
private:
operator SQLHDBC& ();
/// Conversion operator into reference to native type.
SQLHDBC& handle();
/// Returns reference to handle;
ConnectionHandle(const ConnectionHandle&);
const ConnectionHandle& operator=(const ConnectionHandle&);
const EnvironmentHandle* _pEnvironment;
SQLHDBC _hdbc;
bool _ownsEnvironment;
};
//
// inlines
//
inline ConnectionHandle::operator const SQLHDBC& () const
namespace Data
{
return handle();
namespace ODBC
{
class ODBC_API ConnectionHandle
/// ODBC connection handle class
{
public:
ConnectionHandle(EnvironmentHandle * pEnvironment = 0);
/// Creates the ConnectionHandle.
~ConnectionHandle();
/// Creates the ConnectionHandle.
operator const SQLHDBC &() const;
/// Const conversion operator into reference to native type.
const SQLHDBC & handle() const;
/// Returns const reference to handle;
private:
operator SQLHDBC &();
/// Conversion operator into reference to native type.
SQLHDBC & handle();
/// Returns reference to handle;
ConnectionHandle(const ConnectionHandle &);
const ConnectionHandle & operator=(const ConnectionHandle &);
const EnvironmentHandle * _pEnvironment;
SQLHDBC _hdbc;
bool _ownsEnvironment;
};
//
// inlines
//
inline ConnectionHandle::operator const SQLHDBC &() const
{
return handle();
}
inline const SQLHDBC & ConnectionHandle::handle() const
{
return _hdbc;
}
inline ConnectionHandle::operator SQLHDBC &()
{
return handle();
}
inline SQLHDBC & ConnectionHandle::handle()
{
return _hdbc;
}
}
}
inline const SQLHDBC& ConnectionHandle::handle() const
{
return _hdbc;
}
inline ConnectionHandle::operator SQLHDBC& ()
{
return handle();
}
inline SQLHDBC& ConnectionHandle::handle()
{
return _hdbc;
}
} } } // namespace Poco::Data::ODBC
} // namespace Poco::Data::ODBC
#endif

View File

@ -18,79 +18,84 @@
#define Data_ODBC_Connector_INCLUDED
#include "Poco/Data/ODBC/ODBC.h"
#include "Poco/Data/Connector.h"
#include "Poco/Data/ODBC/ODBC.h"
namespace Poco {
namespace Data {
namespace ODBC {
class ODBC_API Connector: public Poco::Data::Connector
/// Connector instantiates SqLite SessionImpl objects.
namespace Poco
{
public:
static const std::string KEY;
/// Keyword for creating ODBC sessions.
Connector();
/// Creates the Connector.
~Connector();
/// Destroys the Connector.
const std::string& name() const;
/// Returns the name associated with this connector.
Poco::AutoPtr<Poco::Data::SessionImpl> createSession(const std::string& connectionString,
std::size_t timeout = Poco::Data::SessionImpl::LOGIN_TIMEOUT_DEFAULT);
/// Creates a ODBC SessionImpl object and initializes it with the given connectionString.
static void registerConnector();
/// Registers the Connector under the Keyword Connector::KEY at the Poco::Data::SessionFactory
static void unregisterConnector();
/// Unregisters the Connector under the Keyword Connector::KEY at the Poco::Data::SessionFactory
static void bindStringToLongVarChar(bool flag = true);
/// If set to true (default), std::string is bound to SQL_LONGVARCHAR.
///
/// This can cause issues with SQL Server, resulting in an error
/// ("The data types varchar and text are incompatible in the equal to operator")
/// when comparing against a VARCHAR.
///
/// Set this to false to bind std::string to SQL_VARCHAR.
///
/// NOTE: This is a global setting, affecting all sessions.
/// This setting should not be changed after the first Session has
/// been created.
static bool stringBoundToLongVarChar();
/// Returns true if std::string is bound to SQL_LONGVARCHAR,
/// otherwise false (bound to SQL_VARCHAR).
private:
static bool _bindStringToLongVarChar;
};
///
/// inlines
///
inline const std::string& Connector::name() const
namespace Data
{
return KEY;
namespace ODBC
{
class ODBC_API Connector : public Poco::Data::Connector
/// Connector instantiates SqLite SessionImpl objects.
{
public:
static const std::string KEY;
/// Keyword for creating ODBC sessions.
Connector();
/// Creates the Connector.
~Connector();
/// Destroys the Connector.
const std::string & name() const;
/// Returns the name associated with this connector.
Poco::AutoPtr<Poco::Data::SessionImpl>
createSession(const std::string & connectionString, std::size_t timeout = Poco::Data::SessionImpl::LOGIN_TIMEOUT_DEFAULT);
/// Creates a ODBC SessionImpl object and initializes it with the given connectionString.
static void registerConnector();
/// Registers the Connector under the Keyword Connector::KEY at the Poco::Data::SessionFactory
static void unregisterConnector();
/// Unregisters the Connector under the Keyword Connector::KEY at the Poco::Data::SessionFactory
static void bindStringToLongVarChar(bool flag = true);
/// If set to true (default), std::string is bound to SQL_LONGVARCHAR.
///
/// This can cause issues with SQL Server, resulting in an error
/// ("The data types varchar and text are incompatible in the equal to operator")
/// when comparing against a VARCHAR.
///
/// Set this to false to bind std::string to SQL_VARCHAR.
///
/// NOTE: This is a global setting, affecting all sessions.
/// This setting should not be changed after the first Session has
/// been created.
static bool stringBoundToLongVarChar();
/// Returns true if std::string is bound to SQL_LONGVARCHAR,
/// otherwise false (bound to SQL_VARCHAR).
private:
static bool _bindStringToLongVarChar;
};
///
/// inlines
///
inline const std::string & Connector::name() const
{
return KEY;
}
inline bool Connector::stringBoundToLongVarChar()
{
return _bindStringToLongVarChar;
}
}
}
inline bool Connector::stringBoundToLongVarChar()
{
return _bindStringToLongVarChar;
}
} } } // namespace Poco::Data::ODBC
} // namespace Poco::Data::ODBC
#endif // Data_ODBC_Connector_INCLUDED

View File

@ -18,222 +18,200 @@
#define Data_ODBC_Diagnostics_INCLUDED
#include "Poco/Data/ODBC/ODBC.h"
#include <vector>
#include <cstring>
#ifdef POCO_OS_FAMILY_WINDOWS
#include <windows.h>
#endif
#include <vector>
#include "Poco/Data/ODBC/ODBC.h"
#include "Poco/Data/ODBC/Utility.h"
#include <sqlext.h>
namespace Poco {
namespace Data {
namespace ODBC {
template <typename H, SQLSMALLINT handleType>
class Diagnostics
/// Utility class providing functionality for retrieving ODBC diagnostic
/// records. Diagnostics object must be created with corresponding handle
/// as constructor argument. During construction, diagnostic records fields
/// are populated and the object is ready for querying.
namespace Poco
{
public:
static const unsigned int SQL_STATE_SIZE = SQL_SQLSTATE_SIZE + 1;
static const unsigned int SQL_MESSAGE_LENGTH = SQL_MAX_MESSAGE_LENGTH + 1;
static const unsigned int SQL_NAME_LENGTH = 128;
static const std::string DATA_TRUNCATED;
struct DiagnosticFields
{
/// SQLGetDiagRec fields
SQLCHAR _sqlState[SQL_STATE_SIZE];
SQLCHAR _message[SQL_MESSAGE_LENGTH];
SQLINTEGER _nativeError;
};
typedef std::vector<DiagnosticFields> FieldVec;
typedef typename FieldVec::const_iterator Iterator;
explicit Diagnostics(const H& handle): _handle(handle)
/// Creates and initializes the Diagnostics.
{
std::memset(_connectionName, 0, sizeof(_connectionName));
std::memset(_serverName, 0, sizeof(_serverName));
diagnostics();
}
~Diagnostics()
/// Destroys the Diagnostics.
{
}
std::string sqlState(int index) const
/// Returns SQL state.
{
poco_assert (index < count());
return std::string((char*) _fields[index]._sqlState);
}
std::string message(int index) const
/// Returns error message.
{
poco_assert (index < count());
return std::string((char*) _fields[index]._message);
}
long nativeError(int index) const
/// Returns native error code.
{
poco_assert (index < count());
return _fields[index]._nativeError;
}
std::string connectionName() const
/// Returns the connection name.
/// If there is no active connection, connection name defaults to NONE.
/// If connection name is not applicable for query context (such as when querying environment handle),
/// connection name defaults to NOT_APPLICABLE.
{
return std::string((char*) _connectionName);
}
std::string serverName() const
/// Returns the server name.
/// If the connection has not been established, server name defaults to NONE.
/// If server name is not applicable for query context (such as when querying environment handle),
/// connection name defaults to NOT_APPLICABLE.
{
return std::string((char*) _serverName);
}
int count() const
/// Returns the number of contained diagnostic records.
{
return (int) _fields.size();
}
void reset()
/// Resets the diagnostic fields container.
{
_fields.clear();
}
const FieldVec& fields() const
{
return _fields;
}
Iterator begin() const
{
return _fields.begin();
}
Iterator end() const
{
return _fields.end();
}
const Diagnostics& diagnostics()
{
DiagnosticFields df;
SQLSMALLINT count = 1;
SQLSMALLINT messageLength = 0;
static const std::string none = "None";
static const std::string na = "Not applicable";
reset();
while (!Utility::isError(SQLGetDiagRec(handleType,
_handle,
count,
df._sqlState,
&df._nativeError,
df._message,
SQL_MESSAGE_LENGTH,
&messageLength)))
{
if (1 == count)
{
// success of the following two calls is optional
// (they fail if connection has not been established yet
// or return empty string if not applicable for the context)
if (Utility::isError(SQLGetDiagField(handleType,
_handle,
count,
SQL_DIAG_CONNECTION_NAME,
_connectionName,
sizeof(_connectionName),
&messageLength)))
{
std::size_t len = sizeof(_connectionName) > none.length() ?
none.length() : sizeof(_connectionName) - 1;
std::memcpy(_connectionName, none.c_str(), len);
}
else if (0 == _connectionName[0])
{
std::size_t len = sizeof(_connectionName) > na.length() ?
na.length() : sizeof(_connectionName) - 1;
std::memcpy(_connectionName, na.c_str(), len);
}
if (Utility::isError(SQLGetDiagField(handleType,
_handle,
count,
SQL_DIAG_SERVER_NAME,
_serverName,
sizeof(_serverName),
&messageLength)))
{
std::size_t len = sizeof(_serverName) > none.length() ?
none.length() : sizeof(_serverName) - 1;
std::memcpy(_serverName, none.c_str(), len);
}
else if (0 == _serverName[0])
{
std::size_t len = sizeof(_serverName) > na.length() ?
na.length() : sizeof(_serverName) - 1;
std::memcpy(_serverName, na.c_str(), len);
}
}
_fields.push_back(df);
std::memset(df._sqlState, 0, SQL_STATE_SIZE);
std::memset(df._message, 0, SQL_MESSAGE_LENGTH);
df._nativeError = 0;
++count;
}
return *this;
}
private:
Diagnostics();
/// SQLGetDiagField fields
SQLCHAR _connectionName[SQL_NAME_LENGTH];
SQLCHAR _serverName[SQL_NAME_LENGTH];
/// Diagnostics container
FieldVec _fields;
/// Context handle
const H& _handle;
};
namespace Data
{
namespace ODBC
{
typedef Diagnostics<SQLHENV, SQL_HANDLE_ENV> EnvironmentDiagnostics;
typedef Diagnostics<SQLHDBC, SQL_HANDLE_DBC> ConnectionDiagnostics;
typedef Diagnostics<SQLHSTMT, SQL_HANDLE_STMT> StatementDiagnostics;
typedef Diagnostics<SQLHDESC, SQL_HANDLE_DESC> DescriptorDiagnostics;
template <typename H, SQLSMALLINT handleType>
class Diagnostics
/// Utility class providing functionality for retrieving ODBC diagnostic
/// records. Diagnostics object must be created with corresponding handle
/// as constructor argument. During construction, diagnostic records fields
/// are populated and the object is ready for querying.
{
public:
static const unsigned int SQL_STATE_SIZE = SQL_SQLSTATE_SIZE + 1;
static const unsigned int SQL_MESSAGE_LENGTH = SQL_MAX_MESSAGE_LENGTH + 1;
static const unsigned int SQL_NAME_LENGTH = 128;
static const std::string DATA_TRUNCATED;
struct DiagnosticFields
{
/// SQLGetDiagRec fields
SQLCHAR _sqlState[SQL_STATE_SIZE];
SQLCHAR _message[SQL_MESSAGE_LENGTH];
SQLINTEGER _nativeError;
};
typedef std::vector<DiagnosticFields> FieldVec;
typedef typename FieldVec::const_iterator Iterator;
explicit Diagnostics(const H & handle) : _handle(handle)
/// Creates and initializes the Diagnostics.
{
std::memset(_connectionName, 0, sizeof(_connectionName));
std::memset(_serverName, 0, sizeof(_serverName));
diagnostics();
}
~Diagnostics()
/// Destroys the Diagnostics.
{
}
std::string sqlState(int index) const
/// Returns SQL state.
{
poco_assert(index < count());
return std::string((char *)_fields[index]._sqlState);
}
std::string message(int index) const
/// Returns error message.
{
poco_assert(index < count());
return std::string((char *)_fields[index]._message);
}
long nativeError(int index) const
/// Returns native error code.
{
poco_assert(index < count());
return _fields[index]._nativeError;
}
std::string connectionName() const
/// Returns the connection name.
/// If there is no active connection, connection name defaults to NONE.
/// If connection name is not applicable for query context (such as when querying environment handle),
/// connection name defaults to NOT_APPLICABLE.
{
return std::string((char *)_connectionName);
}
std::string serverName() const
/// Returns the server name.
/// If the connection has not been established, server name defaults to NONE.
/// If server name is not applicable for query context (such as when querying environment handle),
/// connection name defaults to NOT_APPLICABLE.
{
return std::string((char *)_serverName);
}
int count() const
/// Returns the number of contained diagnostic records.
{
return (int)_fields.size();
}
void reset()
/// Resets the diagnostic fields container.
{
_fields.clear();
}
const FieldVec & fields() const { return _fields; }
Iterator begin() const { return _fields.begin(); }
Iterator end() const { return _fields.end(); }
const Diagnostics & diagnostics()
{
DiagnosticFields df;
SQLSMALLINT count = 1;
SQLSMALLINT messageLength = 0;
static const std::string none = "None";
static const std::string na = "Not applicable";
reset();
while (!Utility::isError(SQLGetDiagRec(
handleType, _handle, count, df._sqlState, &df._nativeError, df._message, SQL_MESSAGE_LENGTH, &messageLength)))
{
if (1 == count)
{
// success of the following two calls is optional
// (they fail if connection has not been established yet
// or return empty string if not applicable for the context)
if (Utility::isError(SQLGetDiagField(
handleType,
_handle,
count,
SQL_DIAG_CONNECTION_NAME,
_connectionName,
sizeof(_connectionName),
&messageLength)))
{
std::size_t len = sizeof(_connectionName) > none.length() ? none.length() : sizeof(_connectionName) - 1;
std::memcpy(_connectionName, none.c_str(), len);
}
else if (0 == _connectionName[0])
{
std::size_t len = sizeof(_connectionName) > na.length() ? na.length() : sizeof(_connectionName) - 1;
std::memcpy(_connectionName, na.c_str(), len);
}
if (Utility::isError(SQLGetDiagField(
handleType, _handle, count, SQL_DIAG_SERVER_NAME, _serverName, sizeof(_serverName), &messageLength)))
{
std::size_t len = sizeof(_serverName) > none.length() ? none.length() : sizeof(_serverName) - 1;
std::memcpy(_serverName, none.c_str(), len);
}
else if (0 == _serverName[0])
{
std::size_t len = sizeof(_serverName) > na.length() ? na.length() : sizeof(_serverName) - 1;
std::memcpy(_serverName, na.c_str(), len);
}
}
_fields.push_back(df);
std::memset(df._sqlState, 0, SQL_STATE_SIZE);
std::memset(df._message, 0, SQL_MESSAGE_LENGTH);
df._nativeError = 0;
++count;
}
return *this;
}
private:
Diagnostics();
/// SQLGetDiagField fields
SQLCHAR _connectionName[SQL_NAME_LENGTH];
SQLCHAR _serverName[SQL_NAME_LENGTH];
/// Diagnostics container
FieldVec _fields;
/// Context handle
const H & _handle;
};
} } } // namespace Poco::Data::ODBC
typedef Diagnostics<SQLHENV, SQL_HANDLE_ENV> EnvironmentDiagnostics;
typedef Diagnostics<SQLHDBC, SQL_HANDLE_DBC> ConnectionDiagnostics;
typedef Diagnostics<SQLHSTMT, SQL_HANDLE_STMT> StatementDiagnostics;
typedef Diagnostics<SQLHDESC, SQL_HANDLE_DESC> DescriptorDiagnostics;
}
}
} // namespace Poco::Data::ODBC
#endif

View File

@ -19,76 +19,78 @@
#include "Poco/Data/ODBC/ODBC.h"
#ifdef POCO_OS_FAMILY_WINDOWS
#include <windows.h>
#endif
#include <sqltypes.h>
namespace Poco {
namespace Data {
namespace ODBC {
class ODBC_API EnvironmentHandle
/// ODBC environment handle class
namespace Poco
{
public:
EnvironmentHandle();
/// Creates the EnvironmentHandle.
~EnvironmentHandle();
/// Destroys the EnvironmentHandle.
operator const SQLHENV& () const;
/// Const conversion operator into reference to native type.
const SQLHENV& handle() const;
/// Returns const reference to handle.
private:
operator SQLHENV& ();
/// Conversion operator into reference to native type.
SQLHENV& handle();
/// Returns reference to handle.
EnvironmentHandle(const EnvironmentHandle&);
const EnvironmentHandle& operator=(const EnvironmentHandle&);
SQLHENV _henv;
bool _isOwner;
};
///
/// inlines
///
inline EnvironmentHandle::operator const SQLHENV& () const
namespace Data
{
return handle();
namespace ODBC
{
class ODBC_API EnvironmentHandle
/// ODBC environment handle class
{
public:
EnvironmentHandle();
/// Creates the EnvironmentHandle.
~EnvironmentHandle();
/// Destroys the EnvironmentHandle.
operator const SQLHENV &() const;
/// Const conversion operator into reference to native type.
const SQLHENV & handle() const;
/// Returns const reference to handle.
private:
operator SQLHENV &();
/// Conversion operator into reference to native type.
SQLHENV & handle();
/// Returns reference to handle.
EnvironmentHandle(const EnvironmentHandle &);
const EnvironmentHandle & operator=(const EnvironmentHandle &);
SQLHENV _henv;
bool _isOwner;
};
///
/// inlines
///
inline EnvironmentHandle::operator const SQLHENV &() const
{
return handle();
}
inline const SQLHENV & EnvironmentHandle::handle() const
{
return _henv;
}
inline EnvironmentHandle::operator SQLHENV &()
{
return handle();
}
inline SQLHENV & EnvironmentHandle::handle()
{
return _henv;
}
}
}
inline const SQLHENV& EnvironmentHandle::handle() const
{
return _henv;
}
inline EnvironmentHandle::operator SQLHENV& ()
{
return handle();
}
inline SQLHENV& EnvironmentHandle::handle()
{
return _henv;
}
} } } // namespace Poco::Data::ODBC
} // namespace Poco::Data::ODBC
#endif

View File

@ -18,107 +18,107 @@
#define Data_ODBC_Error_INCLUDED
#include <vector>
#include "Poco/Data/ODBC/Diagnostics.h"
#include "Poco/Data/ODBC/ODBC.h"
#include "Poco/Data/ODBC/Utility.h"
#include "Poco/Data/ODBC/Diagnostics.h"
#include "Poco/Format.h"
#include <vector>
#ifdef POCO_OS_FAMILY_WINDOWS
#include <windows.h>
#endif
#include <sqlext.h>
namespace Poco {
namespace Data {
namespace ODBC {
template <typename H, SQLSMALLINT handleType>
class Error
/// Class encapsulating ODBC diagnostic record collection. Collection is generated
/// during construction. Class provides access and string generation for the collection
/// as well as individual diagnostic records.
namespace Poco
{
public:
explicit Error(const H& handle) : _diagnostics(handle)
/// Creates the Error.
{
}
~Error()
/// Destroys the Error.
{
}
const Diagnostics<H, handleType>& diagnostics() const
/// Returns the associated diagnostics.
{
return _diagnostics;
}
int count() const
/// Returns the count of diagnostic records.
{
return (int) _diagnostics.count();
}
std::string& toString(int index, std::string& str) const
/// Generates the string for the diagnostic record.
{
if ((index < 0) || (index > (count() - 1)))
return str;
std::string s;
Poco::format(s,
"===========================\n"
"ODBC Diagnostic record #%d:\n"
"===========================\n"
"SQLSTATE = %s\nNative Error Code = %ld\n%s\n\n",
index + 1,
_diagnostics.sqlState(index),
_diagnostics.nativeError(index),
_diagnostics.message(index));
str.append(s);
return str;
}
std::string toString() const
/// Generates the string for the diagnostic record collection.
{
std::string str;
Poco::format(str,
"Connection:%s\nServer:%s\n",
_diagnostics.connectionName(),
_diagnostics.serverName());
std::string s;
for (int i = 0; i < count(); ++i)
{
s.clear();
str.append(toString(i, s));
}
return str;
}
private:
Error();
Diagnostics<H, handleType> _diagnostics;
};
namespace Data
{
namespace ODBC
{
typedef Error<SQLHENV, SQL_HANDLE_ENV> EnvironmentError;
typedef Error<SQLHDBC, SQL_HANDLE_DBC> ConnectionError;
typedef Error<SQLHSTMT, SQL_HANDLE_STMT> StatementError;
typedef Error<SQLHSTMT, SQL_HANDLE_DESC> DescriptorError;
template <typename H, SQLSMALLINT handleType>
class Error
/// Class encapsulating ODBC diagnostic record collection. Collection is generated
/// during construction. Class provides access and string generation for the collection
/// as well as individual diagnostic records.
{
public:
explicit Error(const H & handle) : _diagnostics(handle)
/// Creates the Error.
{
}
~Error()
/// Destroys the Error.
{
}
const Diagnostics<H, handleType> & diagnostics() const
/// Returns the associated diagnostics.
{
return _diagnostics;
}
int count() const
/// Returns the count of diagnostic records.
{
return (int)_diagnostics.count();
}
std::string & toString(int index, std::string & str) const
/// Generates the string for the diagnostic record.
{
if ((index < 0) || (index > (count() - 1)))
return str;
std::string s;
Poco::format(
s,
"===========================\n"
"ODBC Diagnostic record #%d:\n"
"===========================\n"
"SQLSTATE = %s\nNative Error Code = %ld\n%s\n\n",
index + 1,
_diagnostics.sqlState(index),
_diagnostics.nativeError(index),
_diagnostics.message(index));
str.append(s);
return str;
}
std::string toString() const
/// Generates the string for the diagnostic record collection.
{
std::string str;
Poco::format(str, "Connection:%s\nServer:%s\n", _diagnostics.connectionName(), _diagnostics.serverName());
std::string s;
for (int i = 0; i < count(); ++i)
{
s.clear();
str.append(toString(i, s));
}
return str;
}
private:
Error();
Diagnostics<H, handleType> _diagnostics;
};
} } } // namespace Poco::Data::ODBC
typedef Error<SQLHENV, SQL_HANDLE_ENV> EnvironmentError;
typedef Error<SQLHDBC, SQL_HANDLE_DBC> ConnectionError;
typedef Error<SQLHSTMT, SQL_HANDLE_STMT> StatementError;
typedef Error<SQLHSTMT, SQL_HANDLE_DESC> DescriptorError;
}
}
} // namespace Poco::Data::ODBC
#endif

File diff suppressed because it is too large Load Diff

View File

@ -18,96 +18,94 @@
#define Data_ODBC_Handle_INCLUDED
#include "Poco/Data/ODBC/ODBC.h"
#include "Poco/Data/ODBC/EnvironmentHandle.h"
#include "Poco/Data/ODBC/ConnectionHandle.h"
#include "Poco/Data/ODBC/EnvironmentHandle.h"
#include "Poco/Data/ODBC/ODBC.h"
#include "Poco/Data/ODBC/ODBCException.h"
#include "Poco/Data/ODBC/Utility.h"
#ifdef POCO_OS_FAMILY_WINDOWS
#include <windows.h>
#endif
#include <sqltypes.h>
namespace Poco {
namespace Data {
namespace ODBC {
template <typename H, SQLSMALLINT handleType>
class Handle
/// ODBC handle class template
namespace Poco
{
public:
Handle(const ConnectionHandle& rConnection):
_rConnection(rConnection),
_handle(0)
/// Creates the Handle.
{
if (Utility::isError(SQLAllocHandle(handleType,
_rConnection,
&_handle)))
{
throw ODBCException("Could not allocate statement handle.");
}
}
~Handle()
/// Destroys the Handle.
{
try
{
SQLRETURN rc = SQLFreeHandle(handleType, _handle);
// N.B. Destructors should not throw, but neither do we want to
// leak resources. So, we throw here in debug mode if things go bad.
poco_assert_dbg (!Utility::isError(rc));
}
catch (...)
{
poco_unexpected();
}
}
operator const H& () const
/// Const conversion operator into reference to native type.
{
return handle();
}
const H& handle() const
/// Returns const reference to native type.
{
return _handle;
}
private:
Handle(const Handle&);
const Handle& operator=(const Handle&);
operator H& ()
/// Conversion operator into reference to native type.
{
return handle();
}
H& handle()
/// Returns reference to native type.
{
return _handle;
}
const ConnectionHandle& _rConnection;
H _handle;
friend class ODBCStatementImpl;
};
namespace Data
{
namespace ODBC
{
typedef Handle<SQLHSTMT, SQL_HANDLE_STMT> StatementHandle;
typedef Handle<SQLHDESC, SQL_HANDLE_DESC> DescriptorHandle;
template <typename H, SQLSMALLINT handleType>
class Handle
/// ODBC handle class template
{
public:
Handle(const ConnectionHandle & rConnection) : _rConnection(rConnection), _handle(0)
/// Creates the Handle.
{
if (Utility::isError(SQLAllocHandle(handleType, _rConnection, &_handle)))
{
throw ODBCException("Could not allocate statement handle.");
}
}
~Handle()
/// Destroys the Handle.
{
try
{
SQLRETURN rc = SQLFreeHandle(handleType, _handle);
// N.B. Destructors should not throw, but neither do we want to
// leak resources. So, we throw here in debug mode if things go bad.
poco_assert_dbg(!Utility::isError(rc));
}
catch (...)
{
poco_unexpected();
}
}
operator const H &() const
/// Const conversion operator into reference to native type.
{
return handle();
}
const H & handle() const
/// Returns const reference to native type.
{
return _handle;
}
private:
Handle(const Handle &);
const Handle & operator=(const Handle &);
operator H &()
/// Conversion operator into reference to native type.
{
return handle();
}
H & handle()
/// Returns reference to native type.
{
return _handle;
}
const ConnectionHandle & _rConnection;
H _handle;
friend class ODBCStatementImpl;
};
} } } // namespace Poco::Data::ODBC
typedef Handle<SQLHSTMT, SQL_HANDLE_STMT> StatementHandle;
typedef Handle<SQLHDESC, SQL_HANDLE_DESC> DescriptorHandle;
}
}
} // namespace Poco::Data::ODBC
#endif

View File

@ -21,9 +21,6 @@
#include "Poco/Foundation.h"
#ifdef POCO_OS_FAMILY_WINDOWS
#include <windows.h>
#endif
//
@ -34,36 +31,23 @@
// ODBC_API functions as being imported from a DLL, whereas this DLL sees symbols
// defined with this macro as being exported.
//
#if defined(_WIN32) && defined(POCO_DLL)
#if defined(ODBC_EXPORTS)
#define ODBC_API __declspec(dllexport)
#else
#define ODBC_API __declspec(dllimport)
#endif
#endif
#if !defined(ODBC_API)
#if !defined(POCO_NO_GCC_API_ATTRIBUTE) && defined (__GNUC__) && (__GNUC__ >= 4)
#define ODBC_API __attribute__ ((visibility ("default")))
#else
#define ODBC_API
#endif
# if !defined(POCO_NO_GCC_API_ATTRIBUTE) && defined(__GNUC__) && (__GNUC__ >= 4)
# define ODBC_API __attribute__((visibility("default")))
# else
# define ODBC_API
# endif
#endif
#include "Poco/Data/ODBC/Unicode.h"
//
// Automatically link Data library.
//
#if defined(_MSC_VER)
#if !defined(POCO_NO_AUTOMATIC_LIBS) && !defined(ODBC_EXPORTS)
#pragma comment(lib, "PocoDataODBC" POCO_LIB_SUFFIX)
#endif
#endif
#endif // ODBC_ODBC_INCLUDED

View File

@ -18,133 +18,130 @@
#define Data_ODBC_ODBCException_INCLUDED
#include "Poco/Data/ODBC/ODBC.h"
#include "Poco/Data/ODBC/Utility.h"
#include "Poco/Data/DataException.h"
#include "Poco/Data/ODBC/Diagnostics.h"
#include "Poco/Data/ODBC/Error.h"
#include "Poco/Data/DataException.h"
#include "Poco/Data/ODBC/ODBC.h"
#include "Poco/Data/ODBC/Utility.h"
#include "Poco/Format.h"
namespace Poco {
namespace Data {
namespace ODBC {
POCO_DECLARE_EXCEPTION(ODBC_API, ODBCException, Poco::Data::DataException)
POCO_DECLARE_EXCEPTION(ODBC_API, InsufficientStorageException, ODBCException)
POCO_DECLARE_EXCEPTION(ODBC_API, UnknownDataLengthException, ODBCException)
POCO_DECLARE_EXCEPTION(ODBC_API, DataTruncatedException, ODBCException)
template <class H, SQLSMALLINT handleType>
class HandleException: public ODBCException
namespace Poco
{
public:
HandleException(const H& handle): _error(handle)
/// Creates HandleException
{
message(_error.toString());
}
HandleException(const H& handle, const std::string& msg):
ODBCException(msg),
_error(handle)
/// Creates HandleException
{
extendedMessage(_error.toString());
}
HandleException(const H& handle, const std::string& msg, const std::string& arg):
ODBCException(msg, arg),
_error(handle)
/// Creates HandleException
{
}
HandleException(const H& handle, const std::string& msg, const Poco::Exception& exc):
ODBCException(msg, exc),
_error(handle)
/// Creates HandleException
{
}
HandleException(const HandleException& exc):
ODBCException(exc),
_error(exc._error)
/// Creates HandleException
{
}
~HandleException() throw()
/// Destroys HandleException
{
}
HandleException& operator = (const HandleException& exc)
/// Assignment operator
{
if (&exc != this) _error = exc._error;
return *this;
}
const char* name() const throw()
/// Returns the name of the exception
{
return "ODBC handle exception";
}
const char* className() const throw()
/// Returns the HandleException class name.
{
return typeid(*this).name();
}
Poco::Exception* clone() const
/// Clones the HandleException
{
return new HandleException(*this);
}
void rethrow() const
/// Re-throws the HandleException.
{
throw *this;
}
const Diagnostics<H, handleType>& diagnostics() const
/// Returns error diagnostics.
{
return _error.diagnostics();
}
std::string toString() const
/// Returns the formatted error diagnostics for the handle.
{
return Poco::format("ODBC Error: %s\n===================\n%s\n",
std::string(what()),
_error.toString());
}
static std::string errorString(const H& handle)
/// Returns the error diagnostics string for the handle.
{
return Error<H, handleType>(handle).toString();
}
private:
Error<H, handleType> _error;
};
namespace Data
{
namespace ODBC
{
typedef HandleException<SQLHENV, SQL_HANDLE_ENV> EnvironmentException;
typedef HandleException<SQLHDBC, SQL_HANDLE_DBC> ConnectionException;
typedef HandleException<SQLHSTMT, SQL_HANDLE_STMT> StatementException;
typedef HandleException<SQLHDESC, SQL_HANDLE_DESC> DescriptorException;
POCO_DECLARE_EXCEPTION(ODBC_API, ODBCException, Poco::Data::DataException)
POCO_DECLARE_EXCEPTION(ODBC_API, InsufficientStorageException, ODBCException)
POCO_DECLARE_EXCEPTION(ODBC_API, UnknownDataLengthException, ODBCException)
POCO_DECLARE_EXCEPTION(ODBC_API, DataTruncatedException, ODBCException)
} } } // namespace Poco::Data::ODBC
template <class H, SQLSMALLINT handleType>
class HandleException : public ODBCException
{
public:
HandleException(const H & handle) : _error(handle)
/// Creates HandleException
{
message(_error.toString());
}
HandleException(const H & handle, const std::string & msg) : ODBCException(msg), _error(handle)
/// Creates HandleException
{
extendedMessage(_error.toString());
}
HandleException(const H & handle, const std::string & msg, const std::string & arg) : ODBCException(msg, arg), _error(handle)
/// Creates HandleException
{
}
HandleException(const H & handle, const std::string & msg, const Poco::Exception & exc)
: ODBCException(msg, exc), _error(handle)
/// Creates HandleException
{
}
HandleException(const HandleException & exc) : ODBCException(exc), _error(exc._error)
/// Creates HandleException
{
}
~HandleException() throw()
/// Destroys HandleException
{
}
HandleException & operator=(const HandleException & exc)
/// Assignment operator
{
if (&exc != this)
_error = exc._error;
return *this;
}
const char * name() const throw()
/// Returns the name of the exception
{
return "ODBC handle exception";
}
const char * className() const throw()
/// Returns the HandleException class name.
{
return typeid(*this).name();
}
Poco::Exception * clone() const
/// Clones the HandleException
{
return new HandleException(*this);
}
void rethrow() const
/// Re-throws the HandleException.
{
throw *this;
}
const Diagnostics<H, handleType> & diagnostics() const
/// Returns error diagnostics.
{
return _error.diagnostics();
}
std::string toString() const
/// Returns the formatted error diagnostics for the handle.
{
return Poco::format("ODBC Error: %s\n===================\n%s\n", std::string(what()), _error.toString());
}
static std::string errorString(const H & handle)
/// Returns the error diagnostics string for the handle.
{
return Error<H, handleType>(handle).toString();
}
private:
Error<H, handleType> _error;
};
typedef HandleException<SQLHENV, SQL_HANDLE_ENV> EnvironmentException;
typedef HandleException<SQLHDBC, SQL_HANDLE_DBC> ConnectionException;
typedef HandleException<SQLHSTMT, SQL_HANDLE_STMT> StatementException;
typedef HandleException<SQLHDESC, SQL_HANDLE_DESC> DescriptorException;
}
}
} // namespace Poco::Data::ODBC
#endif

View File

@ -18,75 +18,77 @@
#define Data_ODBC_ODBCColumn_INCLUDED
#include "Poco/Data/ODBC/ODBC.h"
#include "Poco/Data/MetaColumn.h"
#include "Poco/Data/ODBC/Error.h"
#include "Poco/Data/ODBC/Handle.h"
#include "Poco/Data/ODBC/ODBC.h"
#include "Poco/Data/ODBC/ODBCException.h"
#include "Poco/Data/MetaColumn.h"
#ifdef POCO_OS_FAMILY_WINDOWS
#include <windows.h>
#endif
#include <sqlext.h>
namespace Poco {
namespace Data {
namespace ODBC {
class ODBC_API ODBCMetaColumn: public MetaColumn
namespace Poco
{
public:
explicit ODBCMetaColumn(const StatementHandle& rStmt, std::size_t position);
/// Creates the ODBCMetaColumn.
~ODBCMetaColumn();
/// Destroys the ODBCMetaColumn.
std::size_t dataLength() const;
/// A numeric value that is either the maximum or actual character length of a character
/// string or binary data type. It is the maximum character length for a fixed-length data type,
/// or the actual character length for a variable-length data type. Its value always excludes the
/// null-termination byte that ends the character string.
/// This information is returned from the SQL_DESC_LENGTH record field of the IRD.
bool isUnsigned() const;
/// Returns true if column is unsigned or a non-numeric data type.
private:
ODBCMetaColumn();
static const int NAME_BUFFER_LENGTH = 2048;
struct ColumnDescription
{
SQLCHAR name[NAME_BUFFER_LENGTH];
SQLSMALLINT nameBufferLength;
SQLSMALLINT dataType;
SQLULEN size;
SQLSMALLINT decimalDigits;
SQLSMALLINT isNullable;
};
void init();
void getDescription();
SQLLEN _dataLength;
const StatementHandle& _rStmt;
ColumnDescription _columnDesc;
};
///
/// inlines
///
inline std::size_t ODBCMetaColumn::dataLength() const
namespace Data
{
return _dataLength;
namespace ODBC
{
class ODBC_API ODBCMetaColumn : public MetaColumn
{
public:
explicit ODBCMetaColumn(const StatementHandle & rStmt, std::size_t position);
/// Creates the ODBCMetaColumn.
~ODBCMetaColumn();
/// Destroys the ODBCMetaColumn.
std::size_t dataLength() const;
/// A numeric value that is either the maximum or actual character length of a character
/// string or binary data type. It is the maximum character length for a fixed-length data type,
/// or the actual character length for a variable-length data type. Its value always excludes the
/// null-termination byte that ends the character string.
/// This information is returned from the SQL_DESC_LENGTH record field of the IRD.
bool isUnsigned() const;
/// Returns true if column is unsigned or a non-numeric data type.
private:
ODBCMetaColumn();
static const int NAME_BUFFER_LENGTH = 2048;
struct ColumnDescription
{
SQLCHAR name[NAME_BUFFER_LENGTH];
SQLSMALLINT nameBufferLength;
SQLSMALLINT dataType;
SQLULEN size;
SQLSMALLINT decimalDigits;
SQLSMALLINT isNullable;
};
void init();
void getDescription();
SQLLEN _dataLength;
const StatementHandle & _rStmt;
ColumnDescription _columnDesc;
};
///
/// inlines
///
inline std::size_t ODBCMetaColumn::dataLength() const
{
return _dataLength;
}
}
}
} } } // namespace Poco::Data::ODBC
} // namespace Poco::Data::ODBC
#endif

View File

@ -18,189 +18,191 @@
#define Data_ODBC_ODBCStatementImpl_INCLUDED
#include "Poco/Data/ODBC/ODBC.h"
#include "Poco/Data/ODBC/SessionImpl.h"
#include <sstream>
#include "Poco/Data/Column.h"
#include "Poco/Data/ODBC/Binder.h"
#include "Poco/Data/ODBC/Extractor.h"
#include "Poco/Data/ODBC/Preparator.h"
#include "Poco/Data/ODBC/ODBC.h"
#include "Poco/Data/ODBC/ODBCMetaColumn.h"
#include "Poco/Data/ODBC/Preparator.h"
#include "Poco/Data/ODBC/SessionImpl.h"
#include "Poco/Data/StatementImpl.h"
#include "Poco/Data/Column.h"
#include "Poco/SharedPtr.h"
#include "Poco/Format.h"
#include <sstream>
#ifdef POCO_OS_FAMILY_WINDOWS
#include <windows.h>
#endif
#include "Poco/SharedPtr.h"
#include <sqltypes.h>
namespace Poco {
namespace Data {
namespace ODBC {
class ODBC_API ODBCStatementImpl: public Poco::Data::StatementImpl
/// Implements statement functionality needed for ODBC
namespace Poco
{
public:
ODBCStatementImpl(SessionImpl& rSession);
/// Creates the ODBCStatementImpl.
~ODBCStatementImpl();
/// Destroys the ODBCStatementImpl.
protected:
std::size_t columnsReturned() const;
/// Returns number of columns returned by query.
int affectedRowCount() const;
/// Returns the number of affected rows.
/// Used to find out the number of rows affected by insert or update.
const MetaColumn& metaColumn(std::size_t pos) const;
/// Returns column meta data.
bool hasNext();
/// Returns true if a call to next() will return data.
std::size_t next();
/// Retrieves the next row or set of rows from the resultset.
/// Returns the number of rows retrieved.
/// Will throw, if the resultset is empty.
bool canBind() const;
/// Returns true if a valid statement is set and we can bind.
bool canCompile() const;
/// Returns true if another compile is possible.
void compileImpl();
/// Compiles the statement, doesn't bind yet.
/// Does nothing if the statement has already been compiled.
void bindImpl();
/// Binds all parameters and executes the statement.
AbstractExtraction::ExtractorPtr extractor();
/// Returns the concrete extractor used by the statement.
AbstractBinding::BinderPtr binder();
/// Returns the concrete binder used by the statement.
std::string nativeSQL();
/// Returns the SQL string as modified by the driver.
private:
typedef Poco::Data::AbstractBindingVec Bindings;
typedef Poco::SharedPtr<Binder> BinderPtr;
typedef Poco::Data::AbstractExtractionVec Extractions;
typedef Poco::SharedPtr<Preparator> PreparatorPtr;
typedef std::vector<PreparatorPtr> PreparatorVec;
typedef Poco::SharedPtr<Extractor> ExtractorPtr;
typedef std::vector<ExtractorPtr> ExtractorVec;
typedef std::vector<ODBCMetaColumn*> ColumnPtrVec;
typedef std::vector<ColumnPtrVec> ColumnPtrVecVec;
static const std::string INVALID_CURSOR_STATE;
void clear();
/// Closes the cursor and resets indicator variables.
void doBind();
/// Binds parameters.
void makeInternalExtractors();
/// Creates internal extractors if none were supplied from the user.
bool isStoredProcedure() const;
/// Returns true if SQL is a stored procedure call.
void doPrepare();
/// Prepares placeholders for data returned by statement.
/// It is called during statement compilation for SQL statements
/// returning data. For stored procedures returning datasets,
/// it is called upon the first check for data availability
/// (see hasNext() function).
bool hasData() const;
/// Returns true if statement returns data.
void makeStep();
/// Fetches the next row of data.
bool nextRowReady() const;
/// Returns true if there is a row fetched but not yet extracted.
void putData();
/// Called whenever SQLExecute returns SQL_NEED_DATA. This is expected
/// behavior for PB_AT_EXEC binding mode.
void getData();
void addPreparator();
void fillColumns();
void checkError(SQLRETURN rc, const std::string& msg="");
const SQLHDBC& _rConnection;
const StatementHandle _stmt;
PreparatorVec _preparations;
BinderPtr _pBinder;
ExtractorVec _extractors;
bool _stepCalled;
int _nextResponse;
ColumnPtrVecVec _columnPtrs;
bool _prepared;
mutable std::size_t _affectedRowCount;
bool _canCompile;
};
//
// inlines
//
inline AbstractExtraction::ExtractorPtr ODBCStatementImpl::extractor()
namespace Data
{
poco_assert_dbg (currentDataSet() < _extractors.size());
poco_assert_dbg (_extractors[currentDataSet()]);
return _extractors[currentDataSet()];
namespace ODBC
{
class ODBC_API ODBCStatementImpl : public Poco::Data::StatementImpl
/// Implements statement functionality needed for ODBC
{
public:
ODBCStatementImpl(SessionImpl & rSession);
/// Creates the ODBCStatementImpl.
~ODBCStatementImpl();
/// Destroys the ODBCStatementImpl.
protected:
std::size_t columnsReturned() const;
/// Returns number of columns returned by query.
int affectedRowCount() const;
/// Returns the number of affected rows.
/// Used to find out the number of rows affected by insert or update.
const MetaColumn & metaColumn(std::size_t pos) const;
/// Returns column meta data.
bool hasNext();
/// Returns true if a call to next() will return data.
std::size_t next();
/// Retrieves the next row or set of rows from the resultset.
/// Returns the number of rows retrieved.
/// Will throw, if the resultset is empty.
bool canBind() const;
/// Returns true if a valid statement is set and we can bind.
bool canCompile() const;
/// Returns true if another compile is possible.
void compileImpl();
/// Compiles the statement, doesn't bind yet.
/// Does nothing if the statement has already been compiled.
void bindImpl();
/// Binds all parameters and executes the statement.
AbstractExtraction::ExtractorPtr extractor();
/// Returns the concrete extractor used by the statement.
AbstractBinding::BinderPtr binder();
/// Returns the concrete binder used by the statement.
std::string nativeSQL();
/// Returns the SQL string as modified by the driver.
private:
typedef Poco::Data::AbstractBindingVec Bindings;
typedef Poco::SharedPtr<Binder> BinderPtr;
typedef Poco::Data::AbstractExtractionVec Extractions;
typedef Poco::SharedPtr<Preparator> PreparatorPtr;
typedef std::vector<PreparatorPtr> PreparatorVec;
typedef Poco::SharedPtr<Extractor> ExtractorPtr;
typedef std::vector<ExtractorPtr> ExtractorVec;
typedef std::vector<ODBCMetaColumn *> ColumnPtrVec;
typedef std::vector<ColumnPtrVec> ColumnPtrVecVec;
static const std::string INVALID_CURSOR_STATE;
void clear();
/// Closes the cursor and resets indicator variables.
void doBind();
/// Binds parameters.
void makeInternalExtractors();
/// Creates internal extractors if none were supplied from the user.
bool isStoredProcedure() const;
/// Returns true if SQL is a stored procedure call.
void doPrepare();
/// Prepares placeholders for data returned by statement.
/// It is called during statement compilation for SQL statements
/// returning data. For stored procedures returning datasets,
/// it is called upon the first check for data availability
/// (see hasNext() function).
bool hasData() const;
/// Returns true if statement returns data.
void makeStep();
/// Fetches the next row of data.
bool nextRowReady() const;
/// Returns true if there is a row fetched but not yet extracted.
void putData();
/// Called whenever SQLExecute returns SQL_NEED_DATA. This is expected
/// behavior for PB_AT_EXEC binding mode.
void getData();
void addPreparator();
void fillColumns();
void checkError(SQLRETURN rc, const std::string & msg = "");
const SQLHDBC & _rConnection;
const StatementHandle _stmt;
PreparatorVec _preparations;
BinderPtr _pBinder;
ExtractorVec _extractors;
bool _stepCalled;
int _nextResponse;
ColumnPtrVecVec _columnPtrs;
bool _prepared;
mutable std::size_t _affectedRowCount;
bool _canCompile;
};
//
// inlines
//
inline AbstractExtraction::ExtractorPtr ODBCStatementImpl::extractor()
{
poco_assert_dbg(currentDataSet() < _extractors.size());
poco_assert_dbg(_extractors[currentDataSet()]);
return _extractors[currentDataSet()];
}
inline AbstractBinding::BinderPtr ODBCStatementImpl::binder()
{
poco_assert_dbg(!_pBinder.isNull());
return _pBinder;
}
inline std::size_t ODBCStatementImpl::columnsReturned() const
{
poco_assert_dbg(currentDataSet() < _preparations.size());
poco_assert_dbg(_preparations[currentDataSet()]);
return static_cast<std::size_t>(_preparations[currentDataSet()]->columns());
}
inline bool ODBCStatementImpl::hasData() const
{
return (columnsReturned() > 0);
}
inline bool ODBCStatementImpl::nextRowReady() const
{
return (!Utility::isError(_nextResponse));
}
inline bool ODBCStatementImpl::canCompile() const
{
return _canCompile;
}
}
}
inline AbstractBinding::BinderPtr ODBCStatementImpl::binder()
{
poco_assert_dbg (!_pBinder.isNull());
return _pBinder;
}
inline std::size_t ODBCStatementImpl::columnsReturned() const
{
poco_assert_dbg (currentDataSet() < _preparations.size());
poco_assert_dbg (_preparations[currentDataSet()]);
return static_cast<std::size_t>(_preparations[currentDataSet()]->columns());
}
inline bool ODBCStatementImpl::hasData() const
{
return (columnsReturned() > 0);
}
inline bool ODBCStatementImpl::nextRowReady() const
{
return (!Utility::isError(_nextResponse));
}
inline bool ODBCStatementImpl::canCompile() const
{
return _canCompile;
}
} } } // namespace Poco::Data::ODBC
} // namespace Poco::Data::ODBC
#endif // Data_ODBC_ODBCStatementImpl_INCLUDED

View File

@ -18,94 +18,96 @@
#define Data_ODBC_Parameter_INCLUDED
#include "Poco/Data/ODBC/ODBC.h"
#include "Poco/Data/ODBC/Handle.h"
#ifdef POCO_OS_FAMILY_WINDOWS
#include <windows.h>
#endif
#include "Poco/Data/ODBC/ODBC.h"
#include <sqlext.h>
namespace Poco {
namespace Data {
namespace ODBC {
class ODBC_API Parameter
namespace Poco
{
public:
explicit Parameter(const StatementHandle& rStmt, std::size_t colNum);
/// Creates the Parameter.
~Parameter();
/// Destroys the Parameter.
std::size_t number() const;
/// Returns the column number.
std::size_t dataType() const;
/// Returns the SQL data type.
std::size_t columnSize() const;
/// Returns the the size of the column or expression of the corresponding
/// parameter marker as defined by the data source.
std::size_t decimalDigits() const;
/// Returns the number of decimal digits of the column or expression
/// of the corresponding parameter as defined by the data source.
bool isNullable() const;
/// Returns true if column allows null values, false otherwise.
private:
Parameter();
void init();
SQLSMALLINT _dataType;
SQLULEN _columnSize;
SQLSMALLINT _decimalDigits;
SQLSMALLINT _isNullable;
const StatementHandle& _rStmt;
std::size_t _number;
};
///
/// inlines
///
inline std::size_t Parameter::number() const
namespace Data
{
return _number;
namespace ODBC
{
class ODBC_API Parameter
{
public:
explicit Parameter(const StatementHandle & rStmt, std::size_t colNum);
/// Creates the Parameter.
~Parameter();
/// Destroys the Parameter.
std::size_t number() const;
/// Returns the column number.
std::size_t dataType() const;
/// Returns the SQL data type.
std::size_t columnSize() const;
/// Returns the the size of the column or expression of the corresponding
/// parameter marker as defined by the data source.
std::size_t decimalDigits() const;
/// Returns the number of decimal digits of the column or expression
/// of the corresponding parameter as defined by the data source.
bool isNullable() const;
/// Returns true if column allows null values, false otherwise.
private:
Parameter();
void init();
SQLSMALLINT _dataType;
SQLULEN _columnSize;
SQLSMALLINT _decimalDigits;
SQLSMALLINT _isNullable;
const StatementHandle & _rStmt;
std::size_t _number;
};
///
/// inlines
///
inline std::size_t Parameter::number() const
{
return _number;
}
inline std::size_t Parameter::dataType() const
{
return _dataType;
}
inline std::size_t Parameter::columnSize() const
{
return _columnSize;
}
inline std::size_t Parameter::decimalDigits() const
{
return _decimalDigits;
}
inline bool Parameter::isNullable() const
{
return SQL_NULLABLE == _isNullable;
}
}
}
inline std::size_t Parameter::dataType() const
{
return _dataType;
}
inline std::size_t Parameter::columnSize() const
{
return _columnSize;
}
inline std::size_t Parameter::decimalDigits() const
{
return _decimalDigits;
}
inline bool Parameter::isNullable() const
{
return SQL_NULLABLE == _isNullable;
}
} } } // namespace Poco::Data::ODBC
} // namespace Poco::Data::ODBC
#endif

File diff suppressed because it is too large Load Diff

View File

@ -18,273 +18,277 @@
#define Data_ODBC_SessionImpl_INCLUDED
#include "Poco/Data/ODBC/ODBC.h"
#include "Poco/Data/ODBC/Connector.h"
#include "Poco/Data/ODBC/TypeInfo.h"
#include "Poco/Data/ODBC/Binder.h"
#include "Poco/Data/ODBC/Handle.h"
#include "Poco/Data/ODBC/ODBCException.h"
#include "Poco/Data/AbstractSessionImpl.h"
#include "Poco/SharedPtr.h"
#include "Poco/Data/ODBC/Binder.h"
#include "Poco/Data/ODBC/Connector.h"
#include "Poco/Data/ODBC/Handle.h"
#include "Poco/Data/ODBC/ODBC.h"
#include "Poco/Data/ODBC/ODBCException.h"
#include "Poco/Data/ODBC/TypeInfo.h"
#include "Poco/Mutex.h"
#ifdef POCO_OS_FAMILY_WINDOWS
#include <windows.h>
#endif
#include "Poco/SharedPtr.h"
#include <sqltypes.h>
namespace Poco {
namespace Data {
namespace ODBC {
class ODBC_API SessionImpl: public Poco::Data::AbstractSessionImpl<SessionImpl>
/// Implements SessionImpl interface
namespace Poco
{
public:
static const std::size_t ODBC_MAX_FIELD_SIZE = 1024u;
enum TransactionCapability
{
ODBC_TXN_CAPABILITY_UNKNOWN = -1,
ODBC_TXN_CAPABILITY_FALSE = 0,
ODBC_TXN_CAPABILITY_TRUE = 1
};
SessionImpl(const std::string& connect,
std::size_t loginTimeout,
std::size_t maxFieldSize = ODBC_MAX_FIELD_SIZE,
bool autoBind = true,
bool autoExtract = true);
/// Creates the SessionImpl. Opens a connection to the database.
/// Throws NotConnectedException if connection was not successful.
//@ deprecated
SessionImpl(const std::string& connect,
Poco::Any maxFieldSize = ODBC_MAX_FIELD_SIZE,
bool enforceCapability=false,
bool autoBind = true,
bool autoExtract = true);
/// Creates the SessionImpl. Opens a connection to the database.
~SessionImpl();
/// Destroys the SessionImpl.
Poco::Data::StatementImpl* createStatementImpl();
/// Returns an ODBC StatementImpl
void open(const std::string& connect = "");
/// Opens a connection to the Database
void close();
/// Closes the connection
bool isConnected();
/// Returns true if session is connected
void setConnectionTimeout(std::size_t timeout);
/// Sets the session connection timeout value.
std::size_t getConnectionTimeout();
/// Returns the session connection timeout value.
void begin();
/// Starts a transaction
void commit();
/// Commits and ends a transaction
void rollback();
/// Aborts a transaction
bool isTransaction();
/// Returns true iff a transaction is in progress.
const std::string& connectorName() const;
/// Returns the name of the connector.
bool canTransact();
/// Returns true if connection is transaction-capable.
void setTransactionIsolation(Poco::UInt32 ti);
/// Sets the transaction isolation level.
Poco::UInt32 getTransactionIsolation();
/// Returns the transaction isolation level.
bool hasTransactionIsolation(Poco::UInt32);
/// Returns true iff the transaction isolation level corresponding
/// to the supplied bitmask is supported.
bool isTransactionIsolation(Poco::UInt32);
/// Returns true iff the transaction isolation level corresponds
/// to the supplied bitmask.
void autoCommit(const std::string&, bool val);
/// Sets autocommit property for the session.
bool isAutoCommit(const std::string& name="");
/// Returns autocommit property value.
void autoBind(const std::string&, bool val);
/// Sets automatic binding for the session.
bool isAutoBind(const std::string& name="");
/// Returns true if binding is automatic for this session.
void autoExtract(const std::string&, bool val);
/// Sets automatic extraction for the session.
bool isAutoExtract(const std::string& name="");
/// Returns true if extraction is automatic for this session.
void setMaxFieldSize(const std::string& rName, const Poco::Any& rValue);
/// Sets the max field size (the default used when column size is unknown).
Poco::Any getMaxFieldSize(const std::string& rName="");
/// Returns the max field size (the default used when column size is unknown).
int maxStatementLength();
/// Returns maximum length of SQL statement allowed by driver.
void setQueryTimeout(const std::string&, const Poco::Any& value);
/// Sets the timeout (in seconds) for queries.
/// Value must be of type int.
Poco::Any getQueryTimeout(const std::string&);
/// Returns the timeout (in seconds) for queries,
/// or -1 if no timeout has been set.
int queryTimeout() const;
/// Returns the timeout (in seconds) for queries,
/// or -1 if no timeout has been set.
const ConnectionHandle& dbc() const;
/// Returns the connection handle.
Poco::Any dataTypeInfo(const std::string& rName="");
/// Returns the data types information.
private:
void setDataTypeInfo(const std::string& rName, const Poco::Any& rValue);
/// No-op. Throws InvalidAccessException.
static const int FUNCTIONS = SQL_API_ODBC3_ALL_FUNCTIONS_SIZE;
void checkError(SQLRETURN rc, const std::string& msg="");
Poco::UInt32 getDefaultTransactionIsolation();
Poco::UInt32 transactionIsolation(SQLULEN isolation);
std::string _connector;
const ConnectionHandle _db;
Poco::Any _maxFieldSize;
bool _autoBind;
bool _autoExtract;
TypeInfo _dataTypes;
char _canTransact;
bool _inTransaction;
int _queryTimeout;
Poco::FastMutex _mutex;
};
///
/// inlines
///
inline void SessionImpl::checkError(SQLRETURN rc, const std::string& msg)
namespace Data
{
if (Utility::isError(rc))
throw ConnectionException(_db, msg);
namespace ODBC
{
class ODBC_API SessionImpl : public Poco::Data::AbstractSessionImpl<SessionImpl>
/// Implements SessionImpl interface
{
public:
static const std::size_t ODBC_MAX_FIELD_SIZE = 1024u;
enum TransactionCapability
{
ODBC_TXN_CAPABILITY_UNKNOWN = -1,
ODBC_TXN_CAPABILITY_FALSE = 0,
ODBC_TXN_CAPABILITY_TRUE = 1
};
SessionImpl(
const std::string & connect,
std::size_t loginTimeout,
std::size_t maxFieldSize = ODBC_MAX_FIELD_SIZE,
bool autoBind = true,
bool autoExtract = true);
/// Creates the SessionImpl. Opens a connection to the database.
/// Throws NotConnectedException if connection was not successful.
//@ deprecated
SessionImpl(
const std::string & connect,
Poco::Any maxFieldSize = ODBC_MAX_FIELD_SIZE,
bool enforceCapability = false,
bool autoBind = true,
bool autoExtract = true);
/// Creates the SessionImpl. Opens a connection to the database.
~SessionImpl();
/// Destroys the SessionImpl.
Poco::Data::StatementImpl * createStatementImpl();
/// Returns an ODBC StatementImpl
void open(const std::string & connect = "");
/// Opens a connection to the Database
void close();
/// Closes the connection
bool isConnected();
/// Returns true if session is connected
void setConnectionTimeout(std::size_t timeout);
/// Sets the session connection timeout value.
std::size_t getConnectionTimeout();
/// Returns the session connection timeout value.
void begin();
/// Starts a transaction
void commit();
/// Commits and ends a transaction
void rollback();
/// Aborts a transaction
bool isTransaction();
/// Returns true iff a transaction is in progress.
const std::string & connectorName() const;
/// Returns the name of the connector.
bool canTransact();
/// Returns true if connection is transaction-capable.
void setTransactionIsolation(Poco::UInt32 ti);
/// Sets the transaction isolation level.
Poco::UInt32 getTransactionIsolation();
/// Returns the transaction isolation level.
bool hasTransactionIsolation(Poco::UInt32);
/// Returns true iff the transaction isolation level corresponding
/// to the supplied bitmask is supported.
bool isTransactionIsolation(Poco::UInt32);
/// Returns true iff the transaction isolation level corresponds
/// to the supplied bitmask.
void autoCommit(const std::string &, bool val);
/// Sets autocommit property for the session.
bool isAutoCommit(const std::string & name = "");
/// Returns autocommit property value.
void autoBind(const std::string &, bool val);
/// Sets automatic binding for the session.
bool isAutoBind(const std::string & name = "");
/// Returns true if binding is automatic for this session.
void autoExtract(const std::string &, bool val);
/// Sets automatic extraction for the session.
bool isAutoExtract(const std::string & name = "");
/// Returns true if extraction is automatic for this session.
void setMaxFieldSize(const std::string & rName, const Poco::Any & rValue);
/// Sets the max field size (the default used when column size is unknown).
Poco::Any getMaxFieldSize(const std::string & rName = "");
/// Returns the max field size (the default used when column size is unknown).
int maxStatementLength();
/// Returns maximum length of SQL statement allowed by driver.
void setQueryTimeout(const std::string &, const Poco::Any & value);
/// Sets the timeout (in seconds) for queries.
/// Value must be of type int.
Poco::Any getQueryTimeout(const std::string &);
/// Returns the timeout (in seconds) for queries,
/// or -1 if no timeout has been set.
int queryTimeout() const;
/// Returns the timeout (in seconds) for queries,
/// or -1 if no timeout has been set.
const ConnectionHandle & dbc() const;
/// Returns the connection handle.
Poco::Any dataTypeInfo(const std::string & rName = "");
/// Returns the data types information.
private:
void setDataTypeInfo(const std::string & rName, const Poco::Any & rValue);
/// No-op. Throws InvalidAccessException.
static const int FUNCTIONS = SQL_API_ODBC3_ALL_FUNCTIONS_SIZE;
void checkError(SQLRETURN rc, const std::string & msg = "");
Poco::UInt32 getDefaultTransactionIsolation();
Poco::UInt32 transactionIsolation(SQLULEN isolation);
std::string _connector;
const ConnectionHandle _db;
Poco::Any _maxFieldSize;
bool _autoBind;
bool _autoExtract;
TypeInfo _dataTypes;
char _canTransact;
bool _inTransaction;
int _queryTimeout;
Poco::FastMutex _mutex;
};
///
/// inlines
///
inline void SessionImpl::checkError(SQLRETURN rc, const std::string & msg)
{
if (Utility::isError(rc))
throw ConnectionException(_db, msg);
}
inline const ConnectionHandle & SessionImpl::dbc() const
{
return _db;
}
inline void SessionImpl::setMaxFieldSize(const std::string & rName, const Poco::Any & rValue)
{
_maxFieldSize = rValue;
}
inline Poco::Any SessionImpl::getMaxFieldSize(const std::string & rName)
{
return _maxFieldSize;
}
inline void SessionImpl::setDataTypeInfo(const std::string & rName, const Poco::Any & rValue)
{
throw InvalidAccessException();
}
inline Poco::Any SessionImpl::dataTypeInfo(const std::string & rName)
{
return &_dataTypes;
}
inline void SessionImpl::autoBind(const std::string &, bool val)
{
_autoBind = val;
}
inline bool SessionImpl::isAutoBind(const std::string & name)
{
return _autoBind;
}
inline void SessionImpl::autoExtract(const std::string &, bool val)
{
_autoExtract = val;
}
inline bool SessionImpl::isAutoExtract(const std::string & name)
{
return _autoExtract;
}
inline const std::string & SessionImpl::connectorName() const
{
return _connector;
}
inline bool SessionImpl::isTransactionIsolation(Poco::UInt32 ti)
{
return 0 != (ti & getTransactionIsolation());
}
inline void SessionImpl::setQueryTimeout(const std::string &, const Poco::Any & value)
{
_queryTimeout = Poco::AnyCast<int>(value);
}
inline Poco::Any SessionImpl::getQueryTimeout(const std::string &)
{
return _queryTimeout;
}
inline int SessionImpl::queryTimeout() const
{
return _queryTimeout;
}
}
}
inline const ConnectionHandle& SessionImpl::dbc() const
{
return _db;
}
inline void SessionImpl::setMaxFieldSize(const std::string& rName, const Poco::Any& rValue)
{
_maxFieldSize = rValue;
}
inline Poco::Any SessionImpl::getMaxFieldSize(const std::string& rName)
{
return _maxFieldSize;
}
inline void SessionImpl::setDataTypeInfo(const std::string& rName, const Poco::Any& rValue)
{
throw InvalidAccessException();
}
inline Poco::Any SessionImpl::dataTypeInfo(const std::string& rName)
{
return &_dataTypes;
}
inline void SessionImpl::autoBind(const std::string&, bool val)
{
_autoBind = val;
}
inline bool SessionImpl::isAutoBind(const std::string& name)
{
return _autoBind;
}
inline void SessionImpl::autoExtract(const std::string&, bool val)
{
_autoExtract = val;
}
inline bool SessionImpl::isAutoExtract(const std::string& name)
{
return _autoExtract;
}
inline const std::string& SessionImpl::connectorName() const
{
return _connector;
}
inline bool SessionImpl::isTransactionIsolation(Poco::UInt32 ti)
{
return 0 != (ti & getTransactionIsolation());
}
inline void SessionImpl::setQueryTimeout(const std::string&, const Poco::Any& value)
{
_queryTimeout = Poco::AnyCast<int>(value);
}
inline Poco::Any SessionImpl::getQueryTimeout(const std::string&)
{
return _queryTimeout;
}
inline int SessionImpl::queryTimeout() const
{
return _queryTimeout;
}
} } } // namespace Poco::Data::ODBC
} // namespace Poco::Data::ODBC
#endif // Data_ODBC_SessionImpl_INCLUDED

View File

@ -18,100 +18,104 @@
#define Data_ODBC_DataTypes_INCLUDED
#include "Poco/Data/ODBC/ODBC.h"
#include "Poco/NamedTuple.h"
#include "Poco/DynamicAny.h"
#include <vector>
#include <map>
#ifdef POCO_OS_FAMILY_WINDOWS
#include <windows.h>
#endif
#include <vector>
#include "Poco/Data/ODBC/ODBC.h"
#include "Poco/DynamicAny.h"
#include "Poco/NamedTuple.h"
#include <sqlext.h>
namespace Poco {
namespace Data {
namespace ODBC {
class ODBC_API TypeInfo
/// Datatypes mapping utility class.
///
/// This class provides mapping between C and SQL datatypes as well
/// as datatypes supported by the underlying database. In order for database
/// types to be available, a valid connection handle must be supplied at either
/// object construction time, or at a later point in time, through call to
/// fillTypeInfo member function.
///
/// Class also provides a convenient debugging function that prints
/// tabulated data to an output stream.
namespace Poco
{
public:
typedef std::map<int, int> DataTypeMap;
typedef DataTypeMap::value_type ValueType;
typedef Poco::NamedTuple<std::string,
SQLSMALLINT,
SQLINTEGER,
std::string,
std::string,
std::string,
SQLSMALLINT,
SQLSMALLINT,
SQLSMALLINT,
SQLSMALLINT,
SQLSMALLINT,
SQLSMALLINT,
std::string,
SQLSMALLINT,
SQLSMALLINT,
SQLSMALLINT,
SQLSMALLINT,
SQLINTEGER,
SQLSMALLINT> TypeInfoTup;
typedef std::vector<TypeInfoTup> TypeInfoVec;
explicit TypeInfo(SQLHDBC* pHDBC=0);
/// Creates the TypeInfo.
~TypeInfo();
/// Destroys the TypeInfo.
int cDataType(int sqlDataType) const;
/// Returns C data type corresponding to supplied SQL data type.
int sqlDataType(int cDataType) const;
/// Returns SQL data type corresponding to supplied C data type.
void fillTypeInfo(SQLHDBC pHDBC);
/// Fills the data type info structure for the database.
DynamicAny getInfo(SQLSMALLINT type, const std::string& param) const;
/// Returns information about specified data type as specified by parameter 'type'.
/// The requested information is specified by parameter 'param'.
/// Will fail with a Poco::NotFoundException thrown if the param is not found
bool tryGetInfo(SQLSMALLINT type, const std::string& param, DynamicAny& result) const;
/// Returns information about specified data type as specified by parameter 'type' in param result.
/// The requested information is specified by parameter 'param'.
/// Will return false if the param is not found. The value of result will be not changed in this case.
void print(std::ostream& ostr);
/// Prints all the types (as reported by the underlying database)
/// to the supplied output stream.
private:
void fillCTypes();
void fillSQLTypes();
DataTypeMap _cDataTypes;
DataTypeMap _sqlDataTypes;
TypeInfoVec _typeInfo;
SQLHDBC* _pHDBC;
};
namespace Data
{
namespace ODBC
{
} } } // namespace Poco::Data::ODBC
class ODBC_API TypeInfo
/// Datatypes mapping utility class.
///
/// This class provides mapping between C and SQL datatypes as well
/// as datatypes supported by the underlying database. In order for database
/// types to be available, a valid connection handle must be supplied at either
/// object construction time, or at a later point in time, through call to
/// fillTypeInfo member function.
///
/// Class also provides a convenient debugging function that prints
/// tabulated data to an output stream.
{
public:
typedef std::map<int, int> DataTypeMap;
typedef DataTypeMap::value_type ValueType;
typedef Poco::NamedTuple<
std::string,
SQLSMALLINT,
SQLINTEGER,
std::string,
std::string,
std::string,
SQLSMALLINT,
SQLSMALLINT,
SQLSMALLINT,
SQLSMALLINT,
SQLSMALLINT,
SQLSMALLINT,
std::string,
SQLSMALLINT,
SQLSMALLINT,
SQLSMALLINT,
SQLSMALLINT,
SQLINTEGER,
SQLSMALLINT>
TypeInfoTup;
typedef std::vector<TypeInfoTup> TypeInfoVec;
explicit TypeInfo(SQLHDBC * pHDBC = 0);
/// Creates the TypeInfo.
~TypeInfo();
/// Destroys the TypeInfo.
int cDataType(int sqlDataType) const;
/// Returns C data type corresponding to supplied SQL data type.
int sqlDataType(int cDataType) const;
/// Returns SQL data type corresponding to supplied C data type.
void fillTypeInfo(SQLHDBC pHDBC);
/// Fills the data type info structure for the database.
DynamicAny getInfo(SQLSMALLINT type, const std::string & param) const;
/// Returns information about specified data type as specified by parameter 'type'.
/// The requested information is specified by parameter 'param'.
/// Will fail with a Poco::NotFoundException thrown if the param is not found
bool tryGetInfo(SQLSMALLINT type, const std::string & param, DynamicAny & result) const;
/// Returns information about specified data type as specified by parameter 'type' in param result.
/// The requested information is specified by parameter 'param'.
/// Will return false if the param is not found. The value of result will be not changed in this case.
void print(std::ostream & ostr);
/// Prints all the types (as reported by the underlying database)
/// to the supplied output stream.
private:
void fillCTypes();
void fillSQLTypes();
DataTypeMap _cDataTypes;
DataTypeMap _sqlDataTypes;
TypeInfoVec _typeInfo;
SQLHDBC * _pHDBC;
};
}
}
} // namespace Poco::Data::ODBC
#endif

File diff suppressed because it is too large Load Diff

View File

@ -18,34 +18,39 @@
#define Data_ODBC_Unicode_UNIX_INCLUDED
namespace Poco {
namespace Data {
namespace ODBC {
void makeUTF16(SQLCHAR* pSQLChar, SQLINTEGER length, std::string& target);
/// Utility function for conversion from UTF-8 to UTF-16
inline void makeUTF16(SQLCHAR* pSQLChar, SQLSMALLINT length, std::string& target)
/// Utility function for conversion from UTF-8 to UTF-16.
namespace Poco
{
makeUTF16(pSQLChar, (SQLINTEGER) length, target);
}
void makeUTF8(Poco::Buffer<SQLWCHAR>& buffer, SQLINTEGER length, SQLPOINTER pTarget, SQLINTEGER targetLength);
/// Utility function for conversion from UTF-16 to UTF-8.
inline void makeUTF8(Poco::Buffer<SQLWCHAR>& buffer, int length, SQLPOINTER pTarget, SQLSMALLINT targetLength)
/// Utility function for conversion from UTF-16 to UTF-8.
namespace Data
{
makeUTF8(buffer, length, pTarget, (SQLINTEGER) targetLength);
namespace ODBC
{
void makeUTF16(SQLCHAR * pSQLChar, SQLINTEGER length, std::string & target);
/// Utility function for conversion from UTF-8 to UTF-16
inline void makeUTF16(SQLCHAR * pSQLChar, SQLSMALLINT length, std::string & target)
/// Utility function for conversion from UTF-8 to UTF-16.
{
makeUTF16(pSQLChar, (SQLINTEGER)length, target);
}
void makeUTF8(Poco::Buffer<SQLWCHAR> & buffer, SQLINTEGER length, SQLPOINTER pTarget, SQLINTEGER targetLength);
/// Utility function for conversion from UTF-16 to UTF-8.
inline void makeUTF8(Poco::Buffer<SQLWCHAR> & buffer, int length, SQLPOINTER pTarget, SQLSMALLINT targetLength)
/// Utility function for conversion from UTF-16 to UTF-8.
{
makeUTF8(buffer, length, pTarget, (SQLINTEGER)targetLength);
}
}
}
} } } // namespace Poco::Data::ODBC
} // namespace Poco::Data::ODBC
#endif // Data_ODBC_Unicode_UNIX_INCLUDED

View File

@ -18,40 +18,45 @@
#define Data_ODBC_Unicode_WIN32_INCLUDED
namespace Poco {
namespace Data {
namespace ODBC {
inline void makeUTF16(SQLCHAR* pSQLChar, SQLINTEGER length, std::wstring& target)
/// Utility function for conversion from UTF-8 to UTF-16
namespace Poco
{
int len = length;
if (SQL_NTS == len)
len = (int) std::strlen((const char *) pSQLChar);
UnicodeConverter::toUTF16((const char *) pSQLChar, len, target);
}
inline void makeUTF8(Poco::Buffer<wchar_t>& buffer, SQLINTEGER length, SQLPOINTER pTarget, SQLINTEGER targetLength)
/// Utility function for conversion from UTF-16 to UTF-8. Length is in bytes.
namespace Data
{
if (buffer.sizeBytes() < length)
throw InvalidArgumentException("Specified length exceeds available length.");
else if ((length % 2) != 0)
throw InvalidArgumentException("Length must be an even number.");
namespace ODBC
{
length /= sizeof(wchar_t);
std::string result;
UnicodeConverter::toUTF8(buffer.begin(), length, result);
std::memset(pTarget, 0, targetLength);
std::strncpy((char*) pTarget, result.c_str(), result.size() < targetLength ? result.size() : targetLength);
inline void makeUTF16(SQLCHAR * pSQLChar, SQLINTEGER length, std::wstring & target)
/// Utility function for conversion from UTF-8 to UTF-16
{
int len = length;
if (SQL_NTS == len)
len = (int)std::strlen((const char *)pSQLChar);
UnicodeConverter::toUTF16((const char *)pSQLChar, len, target);
}
inline void makeUTF8(Poco::Buffer<wchar_t> & buffer, SQLINTEGER length, SQLPOINTER pTarget, SQLINTEGER targetLength)
/// Utility function for conversion from UTF-16 to UTF-8. Length is in bytes.
{
if (buffer.sizeBytes() < length)
throw InvalidArgumentException("Specified length exceeds available length.");
else if ((length % 2) != 0)
throw InvalidArgumentException("Length must be an even number.");
length /= sizeof(wchar_t);
std::string result;
UnicodeConverter::toUTF8(buffer.begin(), length, result);
std::memset(pTarget, 0, targetLength);
std::strncpy((char *)pTarget, result.c_str(), result.size() < targetLength ? result.size() : targetLength);
}
}
}
} } } // namespace Poco::Data::ODBC
} // namespace Poco::Data::ODBC
#endif // Data_ODBC_Unicode_WIN32_INCLUDED

View File

@ -18,190 +18,207 @@
#define Data_ODBC_Utility_INCLUDED
#include <map>
#include <sstream>
#include <sqltypes.h>
#include "Poco/Data/Date.h"
#include "Poco/Data/ODBC/ODBC.h"
#include "Poco/Data/ODBC/TypeInfo.h"
#include "Poco/Data/Date.h"
#include "Poco/Data/Time.h"
#include "Poco/DateTime.h"
#include <sstream>
#include <map>
#include <sqltypes.h>
namespace Poco {
namespace Data {
namespace ODBC {
class ODBC_API Utility
/// Various utility functions
namespace Poco
{
public:
typedef std::map<std::string, std::string> DSNMap;
typedef DSNMap DriverMap;
static bool isError(SQLRETURN rc);
/// Returns true if return code is error
static DriverMap& drivers(DriverMap& driverMap);
/// Returns driver-attributes map of available ODBC drivers.
static DSNMap& dataSources(DSNMap& dsnMap);
/// Returns DSN-description map of available ODBC data sources.
template<typename MapType, typename KeyArgType, typename ValueArgType>
static typename MapType::iterator mapInsert(MapType& m, const KeyArgType& k, const ValueArgType& v)
/// Utility map "insert or replace" function (from S. Meyers: Effective STL, Item 24)
{
typename MapType::iterator lb = m.lower_bound(k);
if (lb != m.end() && !(m.key_comp()(k, lb->first)))
{
lb->second = v;
return lb;
}
else
{
typedef typename MapType::value_type MVT;
return m.insert(lb, MVT(k,v));
}
}
static int cDataType(int sqlDataType);
/// Returns C data type corresponding to supplied SQL data type.
static int sqlDataType(int cDataType);
/// Returns SQL data type corresponding to supplied C data type.
static void dateSync(Date& dt, const SQL_DATE_STRUCT& ts);
/// Transfers data from ODBC SQL_DATE_STRUCT to Poco::DateTime.
template <typename T, typename F>
static void dateSync(T& d, const F& ds)
/// Transfers data from ODBC SQL_DATE_STRUCT container to Poco::DateTime container.
{
std::size_t size = ds.size();
if (d.size() != size) d.resize(size);
typename T::iterator dIt = d.begin();
typename F::const_iterator it = ds.begin();
typename F::const_iterator end = ds.end();
for (; it != end; ++it, ++dIt) dateSync(*dIt, *it);
}
static void timeSync(Time& dt, const SQL_TIME_STRUCT& ts);
/// Transfers data from ODBC SQL_TIME_STRUCT to Poco::DateTime.
template <typename T, typename F>
static void timeSync(T& t, const F& ts)
/// Transfers data from ODBC SQL_TIME_STRUCT container to Poco::DateTime container.
{
std::size_t size = ts.size();
if (t.size() != size) t.resize(size);
typename T::iterator dIt = t.begin();
typename F::const_iterator it = ts.begin();
typename F::const_iterator end = ts.end();
for (; it != end; ++it, ++dIt) timeSync(*dIt, *it);
}
static void dateTimeSync(Poco::DateTime& dt, const SQL_TIMESTAMP_STRUCT& ts);
/// Transfers data from ODBC SQL_TIMESTAMP_STRUCT to Poco::DateTime.
template <typename T, typename F>
static void dateTimeSync(T& dt, const F& ts)
/// Transfers data from ODBC SQL_TIMESTAMP_STRUCT container to Poco::DateTime container.
{
std::size_t size = ts.size();
if (dt.size() != size) dt.resize(size);
typename T::iterator dIt = dt.begin();
typename F::const_iterator it = ts.begin();
typename F::const_iterator end = ts.end();
for (; it != end; ++it, ++dIt) dateTimeSync(*dIt, *it);
}
static void dateSync(SQL_DATE_STRUCT& ts, const Date& dt);
/// Transfers data from Poco::Data::Date to ODBC SQL_DATE_STRUCT.
template <typename C>
static void dateSync(std::vector<SQL_DATE_STRUCT>& ds, const C& d)
/// Transfers data from Poco::Data::Date vector to ODBC SQL_DATE_STRUCT container.
{
std::size_t size = d.size();
if (ds.size() != size) ds.resize(size);
std::vector<SQL_DATE_STRUCT>::iterator dIt = ds.begin();
typename C::const_iterator it = d.begin();
typename C::const_iterator end = d.end();
for (; it != end; ++it, ++dIt) dateSync(*dIt, *it);
}
static void timeSync(SQL_TIME_STRUCT& ts, const Time& dt);
/// Transfers data from Poco::Data::Time to ODBC SQL_TIME_STRUCT.
template <typename C>
static void timeSync(std::vector<SQL_TIME_STRUCT>& ts, const C& t)
/// Transfers data from Poco::Data::Time container to ODBC SQL_TIME_STRUCT vector.
{
std::size_t size = t.size();
if (ts.size() != size) ts.resize(size);
std::vector<SQL_TIME_STRUCT>::iterator tIt = ts.begin();
typename C::const_iterator it = t.begin();
typename C::const_iterator end = t.end();
for (; it != end; ++it, ++tIt) timeSync(*tIt, *it);
}
static void dateTimeSync(SQL_TIMESTAMP_STRUCT& ts, const Poco::DateTime& dt);
/// Transfers data from Poco::DateTime to ODBC SQL_TIMESTAMP_STRUCT.
template <typename C>
static void dateTimeSync(std::vector<SQL_TIMESTAMP_STRUCT>& ts, const C& dt)
/// Transfers data from Poco::DateTime to ODBC SQL_TIMESTAMP_STRUCT.
{
std::size_t size = dt.size();
if (ts.size() != size) ts.resize(size);
std::vector<SQL_TIMESTAMP_STRUCT>::iterator tIt = ts.begin();
typename C::const_iterator it = dt.begin();
typename C::const_iterator end = dt.end();
for (; it != end; ++it, ++tIt) dateTimeSync(*tIt, *it);
}
private:
static const TypeInfo _dataTypes;
/// C <==> SQL data type mapping
};
///
/// inlines
///
inline bool Utility::isError(SQLRETURN rc)
namespace Data
{
return (0 != (rc & (~1)));
namespace ODBC
{
class ODBC_API Utility
/// Various utility functions
{
public:
typedef std::map<std::string, std::string> DSNMap;
typedef DSNMap DriverMap;
static bool isError(SQLRETURN rc);
/// Returns true if return code is error
static DriverMap & drivers(DriverMap & driverMap);
/// Returns driver-attributes map of available ODBC drivers.
static DSNMap & dataSources(DSNMap & dsnMap);
/// Returns DSN-description map of available ODBC data sources.
template <typename MapType, typename KeyArgType, typename ValueArgType>
static typename MapType::iterator mapInsert(MapType & m, const KeyArgType & k, const ValueArgType & v)
/// Utility map "insert or replace" function (from S. Meyers: Effective STL, Item 24)
{
typename MapType::iterator lb = m.lower_bound(k);
if (lb != m.end() && !(m.key_comp()(k, lb->first)))
{
lb->second = v;
return lb;
}
else
{
typedef typename MapType::value_type MVT;
return m.insert(lb, MVT(k, v));
}
}
static int cDataType(int sqlDataType);
/// Returns C data type corresponding to supplied SQL data type.
static int sqlDataType(int cDataType);
/// Returns SQL data type corresponding to supplied C data type.
static void dateSync(Date & dt, const SQL_DATE_STRUCT & ts);
/// Transfers data from ODBC SQL_DATE_STRUCT to Poco::DateTime.
template <typename T, typename F>
static void dateSync(T & d, const F & ds)
/// Transfers data from ODBC SQL_DATE_STRUCT container to Poco::DateTime container.
{
std::size_t size = ds.size();
if (d.size() != size)
d.resize(size);
typename T::iterator dIt = d.begin();
typename F::const_iterator it = ds.begin();
typename F::const_iterator end = ds.end();
for (; it != end; ++it, ++dIt)
dateSync(*dIt, *it);
}
static void timeSync(Time & dt, const SQL_TIME_STRUCT & ts);
/// Transfers data from ODBC SQL_TIME_STRUCT to Poco::DateTime.
template <typename T, typename F>
static void timeSync(T & t, const F & ts)
/// Transfers data from ODBC SQL_TIME_STRUCT container to Poco::DateTime container.
{
std::size_t size = ts.size();
if (t.size() != size)
t.resize(size);
typename T::iterator dIt = t.begin();
typename F::const_iterator it = ts.begin();
typename F::const_iterator end = ts.end();
for (; it != end; ++it, ++dIt)
timeSync(*dIt, *it);
}
static void dateTimeSync(Poco::DateTime & dt, const SQL_TIMESTAMP_STRUCT & ts);
/// Transfers data from ODBC SQL_TIMESTAMP_STRUCT to Poco::DateTime.
template <typename T, typename F>
static void dateTimeSync(T & dt, const F & ts)
/// Transfers data from ODBC SQL_TIMESTAMP_STRUCT container to Poco::DateTime container.
{
std::size_t size = ts.size();
if (dt.size() != size)
dt.resize(size);
typename T::iterator dIt = dt.begin();
typename F::const_iterator it = ts.begin();
typename F::const_iterator end = ts.end();
for (; it != end; ++it, ++dIt)
dateTimeSync(*dIt, *it);
}
static void dateSync(SQL_DATE_STRUCT & ts, const Date & dt);
/// Transfers data from Poco::Data::Date to ODBC SQL_DATE_STRUCT.
template <typename C>
static void dateSync(std::vector<SQL_DATE_STRUCT> & ds, const C & d)
/// Transfers data from Poco::Data::Date vector to ODBC SQL_DATE_STRUCT container.
{
std::size_t size = d.size();
if (ds.size() != size)
ds.resize(size);
std::vector<SQL_DATE_STRUCT>::iterator dIt = ds.begin();
typename C::const_iterator it = d.begin();
typename C::const_iterator end = d.end();
for (; it != end; ++it, ++dIt)
dateSync(*dIt, *it);
}
static void timeSync(SQL_TIME_STRUCT & ts, const Time & dt);
/// Transfers data from Poco::Data::Time to ODBC SQL_TIME_STRUCT.
template <typename C>
static void timeSync(std::vector<SQL_TIME_STRUCT> & ts, const C & t)
/// Transfers data from Poco::Data::Time container to ODBC SQL_TIME_STRUCT vector.
{
std::size_t size = t.size();
if (ts.size() != size)
ts.resize(size);
std::vector<SQL_TIME_STRUCT>::iterator tIt = ts.begin();
typename C::const_iterator it = t.begin();
typename C::const_iterator end = t.end();
for (; it != end; ++it, ++tIt)
timeSync(*tIt, *it);
}
static void dateTimeSync(SQL_TIMESTAMP_STRUCT & ts, const Poco::DateTime & dt);
/// Transfers data from Poco::DateTime to ODBC SQL_TIMESTAMP_STRUCT.
template <typename C>
static void dateTimeSync(std::vector<SQL_TIMESTAMP_STRUCT> & ts, const C & dt)
/// Transfers data from Poco::DateTime to ODBC SQL_TIMESTAMP_STRUCT.
{
std::size_t size = dt.size();
if (ts.size() != size)
ts.resize(size);
std::vector<SQL_TIMESTAMP_STRUCT>::iterator tIt = ts.begin();
typename C::const_iterator it = dt.begin();
typename C::const_iterator end = dt.end();
for (; it != end; ++it, ++tIt)
dateTimeSync(*tIt, *it);
}
private:
static const TypeInfo _dataTypes;
/// C <==> SQL data type mapping
};
///
/// inlines
///
inline bool Utility::isError(SQLRETURN rc)
{
return (0 != (rc & (~1)));
}
inline int Utility::cDataType(int sqlDataType)
{
return _dataTypes.cDataType(sqlDataType);
}
inline int Utility::sqlDataType(int cDataType)
{
return _dataTypes.sqlDataType(cDataType);
}
inline void Utility::dateSync(Date & d, const SQL_DATE_STRUCT & ts)
{
d.assign(ts.year, ts.month, ts.day);
}
inline void Utility::timeSync(Time & t, const SQL_TIME_STRUCT & ts)
{
t.assign(ts.hour, ts.minute, ts.second);
}
}
}
inline int Utility::cDataType(int sqlDataType)
{
return _dataTypes.cDataType(sqlDataType);
}
inline int Utility::sqlDataType(int cDataType)
{
return _dataTypes.sqlDataType(cDataType);
}
inline void Utility::dateSync(Date& d, const SQL_DATE_STRUCT& ts)
{
d.assign(ts.year, ts.month, ts.day);
}
inline void Utility::timeSync(Time& t, const SQL_TIME_STRUCT& ts)
{
t.assign(ts.hour, ts.minute, ts.second);
}
} } } // namespace Poco::Data::ODBC
} // namespace Poco::Data::ODBC
#endif

View File

@ -500,18 +500,6 @@ void Binder::getColumnOrParameterSize(std::size_t pos, SQLINTEGER& size)
{
size = DEFAULT_PARAM_SIZE;
//On Linux, PostgreSQL driver segfaults on SQLGetDescField, so this is disabled for now
#ifdef POCO_OS_FAMILY_WINDOWS
SQLHDESC hIPD = 0;
if (!Utility::isError(SQLGetStmtAttr(_rStmt, SQL_ATTR_IMP_PARAM_DESC, &hIPD, SQL_IS_POINTER, 0)))
{
SQLUINTEGER sz = 0;
if (!Utility::isError(SQLGetDescField(hIPD, (SQLSMALLINT) pos + 1, SQL_DESC_LENGTH, &sz, SQL_IS_UINTEGER, 0)) &&
sz > 0)
{
size = sz;
}
}
#endif
}
if (colSize > 0 && paramSize > 0)

View File

@ -20,9 +20,6 @@
#include "Poco/Exception.h"
#ifdef POCO_OS_FAMILY_WINDOWS
#pragma warning(disable:4312)// 'type cast' : conversion from 'std::size_t' to 'SQLPOINTER' of greater size
#endif
using Poco::DataFormatException;

View File

@ -18,356 +18,360 @@
#define Data_AbstractBinder_INCLUDED
#include "Poco/Data/Data.h"
#include "Poco/Data/Date.h"
#include "Poco/Data/Time.h"
#include "Poco/Data/LOB.h"
#include "Poco/DateTime.h"
#include "Poco/Nullable.h"
#include "Poco/Any.h"
#include "Poco/Dynamic/Var.h"
#include "Poco/UTFString.h"
#include <vector>
#include <cstddef>
#include <deque>
#include <list>
#include <cstddef>
#include <vector>
#include "Poco/Any.h"
#include "Poco/Data/Data.h"
#include "Poco/Data/Date.h"
#include "Poco/Data/LOB.h"
#include "Poco/Data/Time.h"
#include "Poco/DateTime.h"
#include "Poco/Dynamic/Var.h"
#include "Poco/Nullable.h"
#include "Poco/UTFString.h"
namespace Poco {
namespace Data {
typedef NullType NullData;
namespace Keywords {
static const NullData null = NULL_GENERIC;
} // namespace Keywords
class Data_API AbstractBinder
/// Interface for Binding data types to placeholders.
namespace Poco
{
namespace Data
{
public:
typedef SharedPtr<AbstractBinder> Ptr;
enum Direction
/// Binding direction for a parameter.
{
PD_IN,
PD_OUT,
PD_IN_OUT
};
AbstractBinder();
/// Creates the AbstractBinder.
typedef NullType NullData;
virtual ~AbstractBinder();
/// Destroys the AbstractBinder.
virtual void bind(std::size_t pos, const Poco::Int8& val, Direction dir = PD_IN) = 0;
/// Binds an Int8.
namespace Keywords
{
virtual void bind(std::size_t pos, const std::vector<Poco::Int8>& val, Direction dir = PD_IN);
/// Binds an Int8 vector.
virtual void bind(std::size_t pos, const std::deque<Poco::Int8>& val, Direction dir = PD_IN);
/// Binds an Int8 deque.
static const NullData null = NULL_GENERIC;
virtual void bind(std::size_t pos, const std::list<Poco::Int8>& val, Direction dir = PD_IN);
/// Binds an Int8 list.
virtual void bind(std::size_t pos, const Poco::UInt8& val, Direction dir = PD_IN) = 0;
/// Binds an UInt8.
} // namespace Keywords
virtual void bind(std::size_t pos, const std::vector<Poco::UInt8>& val, Direction dir = PD_IN);
/// Binds an UInt8 vector.
virtual void bind(std::size_t pos, const std::deque<Poco::UInt8>& val, Direction dir = PD_IN);
/// Binds an UInt8 deque.
class Data_API AbstractBinder
/// Interface for Binding data types to placeholders.
{
public:
typedef SharedPtr<AbstractBinder> Ptr;
virtual void bind(std::size_t pos, const std::list<Poco::UInt8>& val, Direction dir = PD_IN);
/// Binds an UInt8 list.
enum Direction
/// Binding direction for a parameter.
{
PD_IN,
PD_OUT,
PD_IN_OUT
};
virtual void bind(std::size_t pos, const Poco::Int16& val, Direction dir = PD_IN) = 0;
/// Binds an Int16.
AbstractBinder();
/// Creates the AbstractBinder.
virtual void bind(std::size_t pos, const std::vector<Poco::Int16>& val, Direction dir = PD_IN);
/// Binds an Int16 vector.
virtual ~AbstractBinder();
/// Destroys the AbstractBinder.
virtual void bind(std::size_t pos, const std::deque<Poco::Int16>& val, Direction dir = PD_IN);
/// Binds an Int16 deque.
virtual void bind(std::size_t pos, const Poco::Int8 & val, Direction dir = PD_IN) = 0;
/// Binds an Int8.
virtual void bind(std::size_t pos, const std::list<Poco::Int16>& val, Direction dir = PD_IN);
/// Binds an Int16 list.
virtual void bind(std::size_t pos, const std::vector<Poco::Int8> & val, Direction dir = PD_IN);
/// Binds an Int8 vector.
virtual void bind(std::size_t pos, const Poco::UInt16& val, Direction dir = PD_IN) = 0;
/// Binds an UInt16.
virtual void bind(std::size_t pos, const std::deque<Poco::Int8> & val, Direction dir = PD_IN);
/// Binds an Int8 deque.
virtual void bind(std::size_t pos, const std::vector<Poco::UInt16>& val, Direction dir = PD_IN);
/// Binds an UInt16 vector.
virtual void bind(std::size_t pos, const std::list<Poco::Int8> & val, Direction dir = PD_IN);
/// Binds an Int8 list.
virtual void bind(std::size_t pos, const std::deque<Poco::UInt16>& val, Direction dir = PD_IN);
/// Binds an UInt16 deque.
virtual void bind(std::size_t pos, const Poco::UInt8 & val, Direction dir = PD_IN) = 0;
/// Binds an UInt8.
virtual void bind(std::size_t pos, const std::list<Poco::UInt16>& val, Direction dir = PD_IN);
/// Binds an UInt16 list.
virtual void bind(std::size_t pos, const std::vector<Poco::UInt8> & val, Direction dir = PD_IN);
/// Binds an UInt8 vector.
virtual void bind(std::size_t pos, const Poco::Int32& val, Direction dir = PD_IN) = 0;
/// Binds an Int32.
virtual void bind(std::size_t pos, const std::deque<Poco::UInt8> & val, Direction dir = PD_IN);
/// Binds an UInt8 deque.
virtual void bind(std::size_t pos, const std::vector<Poco::Int32>& val, Direction dir = PD_IN);
/// Binds an Int32 vector.
virtual void bind(std::size_t pos, const std::list<Poco::UInt8> & val, Direction dir = PD_IN);
/// Binds an UInt8 list.
virtual void bind(std::size_t pos, const std::deque<Poco::Int32>& val, Direction dir = PD_IN);
/// Binds an Int32 deque.
virtual void bind(std::size_t pos, const Poco::Int16 & val, Direction dir = PD_IN) = 0;
/// Binds an Int16.
virtual void bind(std::size_t pos, const std::list<Poco::Int32>& val, Direction dir = PD_IN);
/// Binds an Int32 list.
virtual void bind(std::size_t pos, const std::vector<Poco::Int16> & val, Direction dir = PD_IN);
/// Binds an Int16 vector.
virtual void bind(std::size_t pos, const Poco::UInt32& val, Direction dir = PD_IN) = 0;
/// Binds an UInt32.
virtual void bind(std::size_t pos, const std::deque<Poco::Int16> & val, Direction dir = PD_IN);
/// Binds an Int16 deque.
virtual void bind(std::size_t pos, const std::vector<Poco::UInt32>& val, Direction dir = PD_IN);
/// Binds an UInt32 vector.
virtual void bind(std::size_t pos, const std::list<Poco::Int16> & val, Direction dir = PD_IN);
/// Binds an Int16 list.
virtual void bind(std::size_t pos, const std::deque<Poco::UInt32>& val, Direction dir = PD_IN);
/// Binds an UInt32 deque.
virtual void bind(std::size_t pos, const Poco::UInt16 & val, Direction dir = PD_IN) = 0;
/// Binds an UInt16.
virtual void bind(std::size_t pos, const std::list<Poco::UInt32>& val, Direction dir = PD_IN);
/// Binds an UInt32 list.
virtual void bind(std::size_t pos, const Poco::Int64& val, Direction dir = PD_IN) = 0;
/// Binds an Int64.
virtual void bind(std::size_t pos, const std::vector<Poco::UInt16> & val, Direction dir = PD_IN);
/// Binds an UInt16 vector.
virtual void bind(std::size_t pos, const std::vector<Poco::Int64>& val, Direction dir = PD_IN);
/// Binds an Int64 vector.
virtual void bind(std::size_t pos, const std::deque<Poco::UInt16> & val, Direction dir = PD_IN);
/// Binds an UInt16 deque.
virtual void bind(std::size_t pos, const std::deque<Poco::Int64>& val, Direction dir = PD_IN);
/// Binds an Int64 deque.
virtual void bind(std::size_t pos, const std::list<Poco::UInt16> & val, Direction dir = PD_IN);
/// Binds an UInt16 list.
virtual void bind(std::size_t pos, const std::list<Poco::Int64>& val, Direction dir = PD_IN);
/// Binds an Int64 list.
virtual void bind(std::size_t pos, const Poco::Int32 & val, Direction dir = PD_IN) = 0;
/// Binds an Int32.
virtual void bind(std::size_t pos, const Poco::UInt64& val, Direction dir = PD_IN) = 0;
/// Binds an UInt64.
virtual void bind(std::size_t pos, const std::vector<Poco::Int32> & val, Direction dir = PD_IN);
/// Binds an Int32 vector.
virtual void bind(std::size_t pos, const std::vector<Poco::UInt64>& val, Direction dir = PD_IN);
/// Binds an UInt64 vector.
virtual void bind(std::size_t pos, const std::deque<Poco::Int32> & val, Direction dir = PD_IN);
/// Binds an Int32 deque.
virtual void bind(std::size_t pos, const std::deque<Poco::UInt64>& val, Direction dir = PD_IN);
/// Binds an UInt64 deque.
virtual void bind(std::size_t pos, const std::list<Poco::Int32> & val, Direction dir = PD_IN);
/// Binds an Int32 list.
virtual void bind(std::size_t pos, const std::list<Poco::UInt64>& val, Direction dir = PD_IN);
/// Binds an UInt64 list.
virtual void bind(std::size_t pos, const Poco::UInt32 & val, Direction dir = PD_IN) = 0;
/// Binds an UInt32.
virtual void bind(std::size_t pos, const std::vector<Poco::UInt32> & val, Direction dir = PD_IN);
/// Binds an UInt32 vector.
virtual void bind(std::size_t pos, const std::deque<Poco::UInt32> & val, Direction dir = PD_IN);
/// Binds an UInt32 deque.
virtual void bind(std::size_t pos, const std::list<Poco::UInt32> & val, Direction dir = PD_IN);
/// Binds an UInt32 list.
virtual void bind(std::size_t pos, const Poco::Int64 & val, Direction dir = PD_IN) = 0;
/// Binds an Int64.
virtual void bind(std::size_t pos, const std::vector<Poco::Int64> & val, Direction dir = PD_IN);
/// Binds an Int64 vector.
virtual void bind(std::size_t pos, const std::deque<Poco::Int64> & val, Direction dir = PD_IN);
/// Binds an Int64 deque.
virtual void bind(std::size_t pos, const std::list<Poco::Int64> & val, Direction dir = PD_IN);
/// Binds an Int64 list.
virtual void bind(std::size_t pos, const Poco::UInt64 & val, Direction dir = PD_IN) = 0;
/// Binds an UInt64.
virtual void bind(std::size_t pos, const std::vector<Poco::UInt64> & val, Direction dir = PD_IN);
/// Binds an UInt64 vector.
virtual void bind(std::size_t pos, const std::deque<Poco::UInt64> & val, Direction dir = PD_IN);
/// Binds an UInt64 deque.
virtual void bind(std::size_t pos, const std::list<Poco::UInt64> & val, Direction dir = PD_IN);
/// Binds an UInt64 list.
#ifndef POCO_LONG_IS_64_BIT
virtual void bind(std::size_t pos, const long& val, Direction dir = PD_IN) = 0;
/// Binds a long.
virtual void bind(std::size_t pos, const long & val, Direction dir = PD_IN) = 0;
/// Binds a long.
virtual void bind(std::size_t pos, const unsigned long& val, Direction dir = PD_IN) = 0;
/// Binds an unsigned long.
virtual void bind(std::size_t pos, const unsigned long & val, Direction dir = PD_IN) = 0;
/// Binds an unsigned long.
virtual void bind(std::size_t pos, const std::vector<long>& val, Direction dir = PD_IN);
/// Binds a long vector.
virtual void bind(std::size_t pos, const std::vector<long> & val, Direction dir = PD_IN);
/// Binds a long vector.
virtual void bind(std::size_t pos, const std::deque<long>& val, Direction dir = PD_IN);
/// Binds a long deque.
virtual void bind(std::size_t pos, const std::deque<long> & val, Direction dir = PD_IN);
/// Binds a long deque.
virtual void bind(std::size_t pos, const std::list<long>& val, Direction dir = PD_IN);
/// Binds a long list.
virtual void bind(std::size_t pos, const std::list<long> & val, Direction dir = PD_IN);
/// Binds a long list.
#endif
virtual void bind(std::size_t pos, const bool& val, Direction dir = PD_IN) = 0;
/// Binds a boolean.
virtual void bind(std::size_t pos, const bool & val, Direction dir = PD_IN) = 0;
/// Binds a boolean.
virtual void bind(std::size_t pos, const std::vector<bool>& val, Direction dir = PD_IN);
/// Binds a boolean vector.
virtual void bind(std::size_t pos, const std::vector<bool> & val, Direction dir = PD_IN);
/// Binds a boolean vector.
virtual void bind(std::size_t pos, const std::deque<bool>& val, Direction dir = PD_IN);
/// Binds a boolean deque.
virtual void bind(std::size_t pos, const std::deque<bool> & val, Direction dir = PD_IN);
/// Binds a boolean deque.
virtual void bind(std::size_t pos, const std::list<bool>& val, Direction dir = PD_IN);
/// Binds a boolean list.
virtual void bind(std::size_t pos, const std::list<bool> & val, Direction dir = PD_IN);
/// Binds a boolean list.
virtual void bind(std::size_t pos, const float& val, Direction dir = PD_IN) = 0;
/// Binds a float.
virtual void bind(std::size_t pos, const float & val, Direction dir = PD_IN) = 0;
/// Binds a float.
virtual void bind(std::size_t pos, const std::vector<float>& val, Direction dir = PD_IN);
/// Binds a float vector.
virtual void bind(std::size_t pos, const std::vector<float> & val, Direction dir = PD_IN);
/// Binds a float vector.
virtual void bind(std::size_t pos, const std::deque<float>& val, Direction dir = PD_IN);
/// Binds a float deque.
virtual void bind(std::size_t pos, const std::deque<float> & val, Direction dir = PD_IN);
/// Binds a float deque.
virtual void bind(std::size_t pos, const std::list<float>& val, Direction dir = PD_IN);
/// Binds a float list.
virtual void bind(std::size_t pos, const std::list<float> & val, Direction dir = PD_IN);
/// Binds a float list.
virtual void bind(std::size_t pos, const double& val, Direction dir = PD_IN) = 0;
/// Binds a double.
virtual void bind(std::size_t pos, const double & val, Direction dir = PD_IN) = 0;
/// Binds a double.
virtual void bind(std::size_t pos, const std::vector<double>& val, Direction dir = PD_IN);
/// Binds a double vector.
virtual void bind(std::size_t pos, const std::vector<double> & val, Direction dir = PD_IN);
/// Binds a double vector.
virtual void bind(std::size_t pos, const std::deque<double>& val, Direction dir = PD_IN);
/// Binds a double deque.
virtual void bind(std::size_t pos, const std::deque<double> & val, Direction dir = PD_IN);
/// Binds a double deque.
virtual void bind(std::size_t pos, const std::list<double>& val, Direction dir = PD_IN);
/// Binds a double list.
virtual void bind(std::size_t pos, const std::list<double> & val, Direction dir = PD_IN);
/// Binds a double list.
virtual void bind(std::size_t pos, const char& val, Direction dir = PD_IN) = 0;
/// Binds a single character.
virtual void bind(std::size_t pos, const char & val, Direction dir = PD_IN) = 0;
/// Binds a single character.
virtual void bind(std::size_t pos, const std::vector<char>& val, Direction dir = PD_IN);
/// Binds a character vector.
virtual void bind(std::size_t pos, const std::vector<char> & val, Direction dir = PD_IN);
/// Binds a character vector.
virtual void bind(std::size_t pos, const std::deque<char>& val, Direction dir = PD_IN);
/// Binds a character deque.
virtual void bind(std::size_t pos, const std::deque<char> & val, Direction dir = PD_IN);
/// Binds a character deque.
virtual void bind(std::size_t pos, const std::list<char>& val, Direction dir = PD_IN);
/// Binds a character list.
virtual void bind(std::size_t pos, const std::list<char> & val, Direction dir = PD_IN);
/// Binds a character list.
virtual void bind(std::size_t pos, const char* const& pVal, Direction dir = PD_IN) = 0;
/// Binds a const char ptr.
virtual void bind(std::size_t pos, const char * const & pVal, Direction dir = PD_IN) = 0;
/// Binds a const char ptr.
virtual void bind(std::size_t pos, const std::string& val, Direction dir = PD_IN) = 0;
/// Binds a string.
virtual void bind(std::size_t pos, const std::string & val, Direction dir = PD_IN) = 0;
/// Binds a string.
virtual void bind(std::size_t pos, const std::vector<std::string>& val, Direction dir = PD_IN);
/// Binds a string vector.
virtual void bind(std::size_t pos, const std::vector<std::string> & val, Direction dir = PD_IN);
/// Binds a string vector.
virtual void bind(std::size_t pos, const std::deque<std::string>& val, Direction dir = PD_IN);
/// Binds a string deque.
virtual void bind(std::size_t pos, const std::deque<std::string> & val, Direction dir = PD_IN);
/// Binds a string deque.
virtual void bind(std::size_t pos, const std::list<std::string>& val, Direction dir = PD_IN);
/// Binds a string list.
virtual void bind(std::size_t pos, const std::list<std::string> & val, Direction dir = PD_IN);
/// Binds a string list.
virtual void bind(std::size_t pos, const UTF16String& val, Direction dir = PD_IN);
/// Binds a UTF-16 Unicode string.
virtual void bind(std::size_t pos, const UTF16String & val, Direction dir = PD_IN);
/// Binds a UTF-16 Unicode string.
virtual void bind(std::size_t pos, const std::vector<UTF16String>& val, Direction dir = PD_IN);
/// Binds a UTF-16 Unicode string vector.
virtual void bind(std::size_t pos, const std::vector<UTF16String> & val, Direction dir = PD_IN);
/// Binds a UTF-16 Unicode string vector.
virtual void bind(std::size_t pos, const std::deque<UTF16String>& val, Direction dir = PD_IN);
/// Binds a UTF-16 Unicode string deque.
virtual void bind(std::size_t pos, const std::deque<UTF16String> & val, Direction dir = PD_IN);
/// Binds a UTF-16 Unicode string deque.
virtual void bind(std::size_t pos, const std::list<UTF16String>& val, Direction dir = PD_IN);
/// Binds a UTF-16 Unicode string list.
virtual void bind(std::size_t pos, const std::list<UTF16String> & val, Direction dir = PD_IN);
/// Binds a UTF-16 Unicode string list.
virtual void bind(std::size_t pos, const BLOB& val, Direction dir = PD_IN) = 0;
/// Binds a BLOB.
virtual void bind(std::size_t pos, const BLOB & val, Direction dir = PD_IN) = 0;
/// Binds a BLOB.
virtual void bind(std::size_t pos, const CLOB& val, Direction dir = PD_IN) = 0;
/// Binds a CLOB.
virtual void bind(std::size_t pos, const CLOB & val, Direction dir = PD_IN) = 0;
/// Binds a CLOB.
virtual void bind(std::size_t pos, const std::vector<BLOB>& val, Direction dir = PD_IN);
/// Binds a BLOB vector.
virtual void bind(std::size_t pos, const std::vector<BLOB> & val, Direction dir = PD_IN);
/// Binds a BLOB vector.
virtual void bind(std::size_t pos, const std::deque<BLOB>& val, Direction dir = PD_IN);
/// Binds a BLOB deque.
virtual void bind(std::size_t pos, const std::deque<BLOB> & val, Direction dir = PD_IN);
/// Binds a BLOB deque.
virtual void bind(std::size_t pos, const std::list<BLOB>& val, Direction dir = PD_IN);
/// Binds a BLOB list.
virtual void bind(std::size_t pos, const std::list<BLOB> & val, Direction dir = PD_IN);
/// Binds a BLOB list.
virtual void bind(std::size_t pos, const std::vector<CLOB>& val, Direction dir = PD_IN);
/// Binds a CLOB vector.
virtual void bind(std::size_t pos, const std::vector<CLOB> & val, Direction dir = PD_IN);
/// Binds a CLOB vector.
virtual void bind(std::size_t pos, const std::deque<CLOB>& val, Direction dir = PD_IN);
/// Binds a CLOB deque.
virtual void bind(std::size_t pos, const std::deque<CLOB> & val, Direction dir = PD_IN);
/// Binds a CLOB deque.
virtual void bind(std::size_t pos, const std::list<CLOB>& val, Direction dir = PD_IN);
/// Binds a CLOB list.
virtual void bind(std::size_t pos, const std::list<CLOB> & val, Direction dir = PD_IN);
/// Binds a CLOB list.
virtual void bind(std::size_t pos, const DateTime& val, Direction dir = PD_IN) = 0;
/// Binds a DateTime.
virtual void bind(std::size_t pos, const DateTime & val, Direction dir = PD_IN) = 0;
/// Binds a DateTime.
virtual void bind(std::size_t pos, const std::vector<DateTime>& val, Direction dir = PD_IN);
/// Binds a DateTime vector.
virtual void bind(std::size_t pos, const std::vector<DateTime> & val, Direction dir = PD_IN);
/// Binds a DateTime vector.
virtual void bind(std::size_t pos, const std::deque<DateTime>& val, Direction dir = PD_IN);
/// Binds a DateTime deque.
virtual void bind(std::size_t pos, const std::deque<DateTime> & val, Direction dir = PD_IN);
/// Binds a DateTime deque.
virtual void bind(std::size_t pos, const std::list<DateTime>& val, Direction dir = PD_IN);
/// Binds a DateTime list.
virtual void bind(std::size_t pos, const std::list<DateTime> & val, Direction dir = PD_IN);
/// Binds a DateTime list.
virtual void bind(std::size_t pos, const Date& val, Direction dir = PD_IN) = 0;
/// Binds a Date.
virtual void bind(std::size_t pos, const Date & val, Direction dir = PD_IN) = 0;
/// Binds a Date.
virtual void bind(std::size_t pos, const std::vector<Date>& val, Direction dir = PD_IN);
/// Binds a Date vector.
virtual void bind(std::size_t pos, const std::vector<Date> & val, Direction dir = PD_IN);
/// Binds a Date vector.
virtual void bind(std::size_t pos, const std::deque<Date>& val, Direction dir = PD_IN);
/// Binds a Date deque.
virtual void bind(std::size_t pos, const std::deque<Date> & val, Direction dir = PD_IN);
/// Binds a Date deque.
virtual void bind(std::size_t pos, const std::list<Date>& val, Direction dir = PD_IN);
/// Binds a Date list.
virtual void bind(std::size_t pos, const std::list<Date> & val, Direction dir = PD_IN);
/// Binds a Date list.
virtual void bind(std::size_t pos, const Time& val, Direction dir = PD_IN) = 0;
/// Binds a Time.
virtual void bind(std::size_t pos, const Time & val, Direction dir = PD_IN) = 0;
/// Binds a Time.
virtual void bind(std::size_t pos, const std::vector<Time>& val, Direction dir = PD_IN);
/// Binds a Time vector.
virtual void bind(std::size_t pos, const std::vector<Time> & val, Direction dir = PD_IN);
/// Binds a Time vector.
virtual void bind(std::size_t pos, const std::deque<Time>& val, Direction dir = PD_IN);
/// Binds a Time deque.
virtual void bind(std::size_t pos, const std::deque<Time> & val, Direction dir = PD_IN);
/// Binds a Time deque.
virtual void bind(std::size_t pos, const std::list<Time>& val, Direction dir = PD_IN);
/// Binds a Time list.
virtual void bind(std::size_t pos, const std::list<Time> & val, Direction dir = PD_IN);
/// Binds a Time list.
virtual void bind(std::size_t pos, const NullData& val, Direction dir = PD_IN) = 0;
/// Binds a null.
virtual void bind(std::size_t pos, const NullData & val, Direction dir = PD_IN) = 0;
/// Binds a null.
virtual void bind(std::size_t pos, const std::vector<NullData>& val, Direction dir = PD_IN);
/// Binds a null vector.
virtual void bind(std::size_t pos, const std::vector<NullData> & val, Direction dir = PD_IN);
/// Binds a null vector.
virtual void bind(std::size_t pos, const std::deque<NullData>& val, Direction dir = PD_IN);
/// Binds a null deque.
virtual void bind(std::size_t pos, const std::deque<NullData> & val, Direction dir = PD_IN);
/// Binds a null deque.
virtual void bind(std::size_t pos, const std::list<NullData>& val, Direction dir = PD_IN);
/// Binds a null list.
virtual void bind(std::size_t pos, const std::list<NullData> & val, Direction dir = PD_IN);
/// Binds a null list.
void bind(std::size_t pos, const Any& val, Direction dir = PD_IN);
/// Binds an Any.
void bind(std::size_t pos, const Poco::Dynamic::Var& val, Direction dir = PD_IN);
/// Binds a Var.
void bind(std::size_t pos, const Any & val, Direction dir = PD_IN);
/// Binds an Any.
virtual void reset();
/// Resets a binder. No-op by default. Implement for binders that cache data.
void bind(std::size_t pos, const Poco::Dynamic::Var & val, Direction dir = PD_IN);
/// Binds a Var.
static bool isOutBound(Direction dir);
/// Returns true if direction is out bound;
virtual void reset();
/// Resets a binder. No-op by default. Implement for binders that cache data.
static bool isInBound(Direction dir);
/// Returns true if direction is in bound;
};
static bool isOutBound(Direction dir);
/// Returns true if direction is out bound;
static bool isInBound(Direction dir);
/// Returns true if direction is in bound;
};
//
// inlines
//
inline void AbstractBinder::reset()
{
//no-op
}
inline bool AbstractBinder::isOutBound(Direction dir)
{
return PD_OUT == dir || PD_IN_OUT == dir;
}
inline bool AbstractBinder::isInBound(Direction dir)
{
return PD_IN == dir || PD_IN_OUT == dir;
}
//
// inlines
//
inline void AbstractBinder::reset()
{
//no-op
}
inline bool AbstractBinder::isOutBound(Direction dir)
{
return PD_OUT == dir || PD_IN_OUT == dir;
}
inline bool AbstractBinder::isInBound(Direction dir)
{
return PD_IN == dir || PD_IN_OUT == dir;
}
} } // namespace Poco::Data
} // namespace Poco::Data
#endif // Data_AbstractBinder_INCLUDED

View File

@ -18,127 +18,130 @@
#define Data_AbstractBinding_INCLUDED
#include "Poco/Data/Data.h"
#include "Poco/Data/AbstractBinder.h"
#include "Poco/Any.h"
#include "Poco/RefCountedObject.h"
#include "Poco/AutoPtr.h"
#include <vector>
#include <list>
#include <deque>
#include <cstddef>
#include <deque>
#include <list>
#include <vector>
#include "Poco/Any.h"
#include "Poco/AutoPtr.h"
#include "Poco/Data/AbstractBinder.h"
#include "Poco/Data/Data.h"
#include "Poco/RefCountedObject.h"
namespace Poco {
namespace Data {
class Data_API AbstractBinding
/// AbstractBinding connects a value with a placeholder via an AbstractBinder interface.
namespace Poco
{
public:
typedef SharedPtr<AbstractBinding> Ptr;
typedef AbstractBinder::Ptr BinderPtr;
enum Direction
{
PD_IN = AbstractBinder::PD_IN,
PD_OUT = AbstractBinder::PD_OUT,
PD_IN_OUT = AbstractBinder::PD_IN_OUT
};
AbstractBinding(const std::string& name = "", Direction direction = PD_IN, Poco::UInt32 bulkSize = 0);
/// Creates the AbstractBinding.
virtual ~AbstractBinding();
/// Destroys the AbstractBinding.
void setBinder(BinderPtr pBinder);
/// Sets the object used for binding; object does NOT take ownership of the pointer.
BinderPtr getBinder() const;
/// Returns the AbstractBinder used for binding data.
virtual std::size_t numOfColumnsHandled() const = 0;
/// Returns the number of columns that the binding handles.
///
/// The trivial case will be one single column but when
/// complex types are used this value can be larger than one.
virtual std::size_t numOfRowsHandled() const = 0;
/// Returns the number of rows that the binding handles.
///
/// The trivial case will be one single row but
/// for collection data types it can be larger.
virtual bool canBind() const = 0;
/// Returns true if we have enough data to bind
virtual void bind(std::size_t pos) = 0;
/// Binds a value to the given column position
virtual void reset() = 0;
/// Allows a binding to be reused.
AbstractBinder::Direction getDirection() const;
/// Returns the binding direction.
const std::string& name() const;
/// Returns the name for this binding.
bool isBulk() const;
/// Returns true if extraction is bulk.
Poco::UInt32 bulkSize() const;
/// Returns the size of the bulk binding.
private:
BinderPtr _pBinder;
std::string _name;
Direction _direction;
Poco::UInt32 _bulkSize;
};
typedef std::vector<AbstractBinding::Ptr> AbstractBindingVec;
typedef std::deque<AbstractBinding::Ptr> AbstractBindingDeq;
typedef std::list<AbstractBinding::Ptr> AbstractBindingLst;
//
// inlines
//
inline AbstractBinder::Ptr AbstractBinding::getBinder() const
namespace Data
{
return _pBinder;
class Data_API AbstractBinding
/// AbstractBinding connects a value with a placeholder via an AbstractBinder interface.
{
public:
typedef SharedPtr<AbstractBinding> Ptr;
typedef AbstractBinder::Ptr BinderPtr;
enum Direction
{
PD_IN = AbstractBinder::PD_IN,
PD_OUT = AbstractBinder::PD_OUT,
PD_IN_OUT = AbstractBinder::PD_IN_OUT
};
AbstractBinding(const std::string & name = "", Direction direction = PD_IN, Poco::UInt32 bulkSize = 0);
/// Creates the AbstractBinding.
virtual ~AbstractBinding();
/// Destroys the AbstractBinding.
void setBinder(BinderPtr pBinder);
/// Sets the object used for binding; object does NOT take ownership of the pointer.
BinderPtr getBinder() const;
/// Returns the AbstractBinder used for binding data.
virtual std::size_t numOfColumnsHandled() const = 0;
/// Returns the number of columns that the binding handles.
///
/// The trivial case will be one single column but when
/// complex types are used this value can be larger than one.
virtual std::size_t numOfRowsHandled() const = 0;
/// Returns the number of rows that the binding handles.
///
/// The trivial case will be one single row but
/// for collection data types it can be larger.
virtual bool canBind() const = 0;
/// Returns true if we have enough data to bind
virtual void bind(std::size_t pos) = 0;
/// Binds a value to the given column position
virtual void reset() = 0;
/// Allows a binding to be reused.
AbstractBinder::Direction getDirection() const;
/// Returns the binding direction.
const std::string & name() const;
/// Returns the name for this binding.
bool isBulk() const;
/// Returns true if extraction is bulk.
Poco::UInt32 bulkSize() const;
/// Returns the size of the bulk binding.
private:
BinderPtr _pBinder;
std::string _name;
Direction _direction;
Poco::UInt32 _bulkSize;
};
typedef std::vector<AbstractBinding::Ptr> AbstractBindingVec;
typedef std::deque<AbstractBinding::Ptr> AbstractBindingDeq;
typedef std::list<AbstractBinding::Ptr> AbstractBindingLst;
//
// inlines
//
inline AbstractBinder::Ptr AbstractBinding::getBinder() const
{
return _pBinder;
}
inline const std::string & AbstractBinding::name() const
{
return _name;
}
inline AbstractBinder::Direction AbstractBinding::getDirection() const
{
return (AbstractBinder::Direction)_direction;
}
inline bool AbstractBinding::isBulk() const
{
return _bulkSize > 0;
}
inline Poco::UInt32 AbstractBinding::bulkSize() const
{
return _bulkSize;
}
}
inline const std::string& AbstractBinding::name() const
{
return _name;
}
inline AbstractBinder::Direction AbstractBinding::getDirection() const
{
return (AbstractBinder::Direction) _direction;
}
inline bool AbstractBinding::isBulk() const
{
return _bulkSize > 0;
}
inline Poco::UInt32 AbstractBinding::bulkSize() const
{
return _bulkSize;
}
} } // namespace Poco::Data
} // namespace Poco::Data
#endif // Data_AbstractBinding_INCLUDED

View File

@ -18,261 +18,264 @@
#define Data_AbstractExtraction_INCLUDED
#include "Poco/Data/Data.h"
#include <cstddef>
#include <deque>
#include <list>
#include <vector>
#include "Poco/AutoPtr.h"
#include "Poco/Data/AbstractExtractor.h"
#include "Poco/Data/AbstractPreparation.h"
#include "Poco/Data/Data.h"
#include "Poco/Data/Limit.h"
#include "Poco/RefCountedObject.h"
#include "Poco/UTFString.h"
#include "Poco/AutoPtr.h"
#include <vector>
#include <deque>
#include <list>
#include <cstddef>
namespace Poco {
namespace Data {
class AbstractPreparator;
class Data_API AbstractExtraction
/// AbstractExtraction is the interface class that connects output positions to concrete values
/// retrieved via an AbstractExtractor.
namespace Poco
{
public:
typedef SharedPtr<AbstractExtraction> Ptr;
typedef SharedPtr<AbstractExtractor> ExtractorPtr;
typedef SharedPtr<AbstractPreparator> PreparatorPtr;
AbstractExtraction(Poco::UInt32 limit = Limit::LIMIT_UNLIMITED,
Poco::UInt32 position = 0, bool bulk = false);
/// Creates the AbstractExtraction. A limit value equal to EXTRACT_UNLIMITED (0xffffffffu)
/// means that we extract as much data as possible during one execute.
/// Otherwise the limit value is used to partition data extracting to a limited amount of rows.
virtual ~AbstractExtraction();
/// Destroys the AbstractExtraction.
void setExtractor(ExtractorPtr pExtractor);
/// Sets the class used for extracting the data. Does not take ownership of the pointer.
ExtractorPtr getExtractor() const;
/// Retrieves the extractor object
Poco::UInt32 position() const;
/// Returns the extraction position.
virtual std::size_t numOfColumnsHandled() const = 0;
/// Returns the number of columns that the extraction handles.
///
/// The trivial case will be one single column but when
/// complex types are used this value can be larger than one.
virtual std::size_t numOfRowsHandled() const = 0;
/// Returns the number of rows that the extraction handles.
///
/// The trivial case will be one single row but
/// for collection data types (ie vector) it can be larger.
virtual std::size_t numOfRowsAllowed() const = 0;
/// Returns the upper limit on number of rows that the extraction will handle.
virtual std::size_t extract(std::size_t pos) = 0;
/// Extracts a value from the param, starting at the given column position.
/// Returns the number of rows extracted.
virtual void reset();
/// Resets the extractor so that it can be re-used.
/// Does nothing in this implementation.
/// Implementations should override it for different behavior.
virtual bool canExtract() const;
/// Returns true. Implementations should override it for different behavior.
virtual AbstractPreparation::Ptr createPreparation(PreparatorPtr& pPrep, std::size_t pos) = 0;
/// Creates and returns shared pointer to Preparation object for the extracting object.
void setLimit(Poco::UInt32 limit);
/// Sets the limit.
Poco::UInt32 getLimit() const;
/// Gets the limit.
virtual bool isNull(std::size_t row) const;
/// In implementations, this function returns true if value at row is null,
/// false otherwise.
/// Normal behavior is to replace nulls with default values.
/// However, extraction implementations may remember the underlying database
/// null values and be able to later provide information about them.
/// Here, this function throws NotImplementedException.
bool isBulk() const;
/// Returns true if this is bulk extraction.
void setEmptyStringIsNull(bool emptyStringIsNull);
/// Sets the empty string handling flag.
bool getEmptyStringIsNull() const;
/// Returns the empty string handling flag.
void setForceEmptyString(bool forceEmptyString);
/// Sets the force empty string flag.
bool getForceEmptyString() const;
/// Returns the force empty string flag.
template <typename T>
bool isValueNull(const T& str, bool deflt)
/// Utility function to determine the nullness of the value.
/// This generic version always returns default value
/// (i.e. does nothing). The std::string overload does
/// the actual work.
///
{
return deflt;
}
bool isValueNull(const std::string& str, bool deflt);
/// Overload for const reference to std::string.
///
/// Returns true when following conditions are met:
///
/// - string is empty
/// - getEmptyStringIsNull() returns true
bool isValueNull(const Poco::UTF16String& str, bool deflt);
/// Overload for const reference to UTF16String.
///
/// Returns true when following conditions are met:
///
/// - string is empty
/// - getEmptyStringIsNull() returns true
private:
template <typename S>
bool isStringNull(const S& str, bool deflt)
{
if (getForceEmptyString()) return false;
if (getEmptyStringIsNull() && str.empty())
return true;
return deflt;
}
ExtractorPtr _pExtractor;
Poco::UInt32 _limit;
Poco::UInt32 _position;
bool _bulk;
bool _emptyStringIsNull;
bool _forceEmptyString;
};
typedef std::vector<AbstractExtraction::Ptr> AbstractExtractionVec;
typedef std::vector<AbstractExtractionVec> AbstractExtractionVecVec;
typedef std::deque<AbstractExtraction::Ptr> AbstractExtractionDeq;
typedef std::vector<AbstractExtractionDeq> AbstractExtractionDeqVec;
typedef std::list<AbstractExtraction::Ptr> AbstractExtractionLst;
typedef std::vector<AbstractExtractionLst> AbstractExtractionLstVec;
//
// inlines
//
inline void AbstractExtraction::setExtractor(ExtractorPtr pExtractor)
namespace Data
{
_pExtractor = pExtractor;
class AbstractPreparator;
class Data_API AbstractExtraction
/// AbstractExtraction is the interface class that connects output positions to concrete values
/// retrieved via an AbstractExtractor.
{
public:
typedef SharedPtr<AbstractExtraction> Ptr;
typedef SharedPtr<AbstractExtractor> ExtractorPtr;
typedef SharedPtr<AbstractPreparator> PreparatorPtr;
AbstractExtraction(Poco::UInt32 limit = Limit::LIMIT_UNLIMITED, Poco::UInt32 position = 0, bool bulk = false);
/// Creates the AbstractExtraction. A limit value equal to EXTRACT_UNLIMITED (0xffffffffu)
/// means that we extract as much data as possible during one execute.
/// Otherwise the limit value is used to partition data extracting to a limited amount of rows.
virtual ~AbstractExtraction();
/// Destroys the AbstractExtraction.
void setExtractor(ExtractorPtr pExtractor);
/// Sets the class used for extracting the data. Does not take ownership of the pointer.
ExtractorPtr getExtractor() const;
/// Retrieves the extractor object
Poco::UInt32 position() const;
/// Returns the extraction position.
virtual std::size_t numOfColumnsHandled() const = 0;
/// Returns the number of columns that the extraction handles.
///
/// The trivial case will be one single column but when
/// complex types are used this value can be larger than one.
virtual std::size_t numOfRowsHandled() const = 0;
/// Returns the number of rows that the extraction handles.
///
/// The trivial case will be one single row but
/// for collection data types (ie vector) it can be larger.
virtual std::size_t numOfRowsAllowed() const = 0;
/// Returns the upper limit on number of rows that the extraction will handle.
virtual std::size_t extract(std::size_t pos) = 0;
/// Extracts a value from the param, starting at the given column position.
/// Returns the number of rows extracted.
virtual void reset();
/// Resets the extractor so that it can be re-used.
/// Does nothing in this implementation.
/// Implementations should override it for different behavior.
virtual bool canExtract() const;
/// Returns true. Implementations should override it for different behavior.
virtual AbstractPreparation::Ptr createPreparation(PreparatorPtr & pPrep, std::size_t pos) = 0;
/// Creates and returns shared pointer to Preparation object for the extracting object.
void setLimit(Poco::UInt32 limit);
/// Sets the limit.
Poco::UInt32 getLimit() const;
/// Gets the limit.
virtual bool isNull(std::size_t row) const;
/// In implementations, this function returns true if value at row is null,
/// false otherwise.
/// Normal behavior is to replace nulls with default values.
/// However, extraction implementations may remember the underlying database
/// null values and be able to later provide information about them.
/// Here, this function throws NotImplementedException.
bool isBulk() const;
/// Returns true if this is bulk extraction.
void setEmptyStringIsNull(bool emptyStringIsNull);
/// Sets the empty string handling flag.
bool getEmptyStringIsNull() const;
/// Returns the empty string handling flag.
void setForceEmptyString(bool forceEmptyString);
/// Sets the force empty string flag.
bool getForceEmptyString() const;
/// Returns the force empty string flag.
template <typename T>
bool isValueNull(const T & str, bool deflt)
/// Utility function to determine the nullness of the value.
/// This generic version always returns default value
/// (i.e. does nothing). The std::string overload does
/// the actual work.
///
{
return deflt;
}
bool isValueNull(const std::string & str, bool deflt);
/// Overload for const reference to std::string.
///
/// Returns true when following conditions are met:
///
/// - string is empty
/// - getEmptyStringIsNull() returns true
bool isValueNull(const Poco::UTF16String & str, bool deflt);
/// Overload for const reference to UTF16String.
///
/// Returns true when following conditions are met:
///
/// - string is empty
/// - getEmptyStringIsNull() returns true
private:
template <typename S>
bool isStringNull(const S & str, bool deflt)
{
if (getForceEmptyString())
return false;
if (getEmptyStringIsNull() && str.empty())
return true;
return deflt;
}
ExtractorPtr _pExtractor;
Poco::UInt32 _limit;
Poco::UInt32 _position;
bool _bulk;
bool _emptyStringIsNull;
bool _forceEmptyString;
};
typedef std::vector<AbstractExtraction::Ptr> AbstractExtractionVec;
typedef std::vector<AbstractExtractionVec> AbstractExtractionVecVec;
typedef std::deque<AbstractExtraction::Ptr> AbstractExtractionDeq;
typedef std::vector<AbstractExtractionDeq> AbstractExtractionDeqVec;
typedef std::list<AbstractExtraction::Ptr> AbstractExtractionLst;
typedef std::vector<AbstractExtractionLst> AbstractExtractionLstVec;
//
// inlines
//
inline void AbstractExtraction::setExtractor(ExtractorPtr pExtractor)
{
_pExtractor = pExtractor;
}
inline AbstractExtraction::ExtractorPtr AbstractExtraction::getExtractor() const
{
return _pExtractor;
}
inline void AbstractExtraction::setLimit(Poco::UInt32 limit)
{
_limit = limit;
}
inline Poco::UInt32 AbstractExtraction::getLimit() const
{
return _limit;
}
inline bool AbstractExtraction::isNull(std::size_t row) const
{
throw NotImplementedException("Check for null values not implemented.");
}
inline Poco::UInt32 AbstractExtraction::position() const
{
return _position;
}
inline bool AbstractExtraction::isBulk() const
{
return _bulk;
}
inline void AbstractExtraction::reset()
{
}
inline bool AbstractExtraction::canExtract() const
{
return true;
}
inline void AbstractExtraction::setEmptyStringIsNull(bool emptyStringIsNull)
{
_emptyStringIsNull = emptyStringIsNull;
}
inline bool AbstractExtraction::getEmptyStringIsNull() const
{
return _emptyStringIsNull;
}
inline void AbstractExtraction::setForceEmptyString(bool forceEmptyString)
{
_forceEmptyString = forceEmptyString;
}
inline bool AbstractExtraction::getForceEmptyString() const
{
return _forceEmptyString;
}
inline bool AbstractExtraction::isValueNull(const std::string & str, bool deflt)
{
return isStringNull(str, deflt);
}
inline bool AbstractExtraction::isValueNull(const Poco::UTF16String & str, bool deflt)
{
return isStringNull(str, deflt);
}
}
inline AbstractExtraction::ExtractorPtr AbstractExtraction::getExtractor() const
{
return _pExtractor;
}
inline void AbstractExtraction::setLimit(Poco::UInt32 limit)
{
_limit = limit;
}
inline Poco::UInt32 AbstractExtraction::getLimit() const
{
return _limit;
}
inline bool AbstractExtraction::isNull(std::size_t row) const
{
throw NotImplementedException("Check for null values not implemented.");
}
inline Poco::UInt32 AbstractExtraction::position() const
{
return _position;
}
inline bool AbstractExtraction::isBulk() const
{
return _bulk;
}
inline void AbstractExtraction::reset()
{
}
inline bool AbstractExtraction::canExtract() const
{
return true;
}
inline void AbstractExtraction::setEmptyStringIsNull(bool emptyStringIsNull)
{
_emptyStringIsNull = emptyStringIsNull;
}
inline bool AbstractExtraction::getEmptyStringIsNull() const
{
return _emptyStringIsNull;
}
inline void AbstractExtraction::setForceEmptyString(bool forceEmptyString)
{
_forceEmptyString = forceEmptyString;
}
inline bool AbstractExtraction::getForceEmptyString() const
{
return _forceEmptyString;
}
inline bool AbstractExtraction::isValueNull(const std::string& str, bool deflt)
{
return isStringNull(str, deflt);
}
inline bool AbstractExtraction::isValueNull(const Poco::UTF16String& str, bool deflt)
{
return isStringNull(str, deflt);
}
} } // namespace Poco::Data
} // namespace Poco::Data
#endif // Data_AbstractExtraction_INCLUDED

View File

@ -18,334 +18,338 @@
#define Data_AbstractExtractor_INCLUDED
#include "Poco/Data/Data.h"
#include "Poco/Data/Constants.h"
#include "Poco/Data/LOB.h"
#include "Poco/UTFString.h"
#include <vector>
#include <cstddef>
#include <deque>
#include <list>
#include <string>
#include <cstddef>
#include <vector>
#include "Poco/Data/Constants.h"
#include "Poco/Data/Data.h"
#include "Poco/Data/LOB.h"
#include "Poco/UTFString.h"
namespace Poco {
namespace Poco
{
class DateTime;
class Any;
namespace Dynamic {
class Var;
namespace Dynamic
{
class Var;
}
namespace Data {
class Date;
class Time;
class Data_API AbstractExtractor
/// Interface used to extract data from a single result row.
/// If an extractor receives null it is not allowed to change val!
namespace Data
{
public:
typedef SharedPtr<AbstractExtractor> Ptr;
AbstractExtractor();
/// Creates the AbstractExtractor.
virtual ~AbstractExtractor();
/// Destroys the AbstractExtractor.
class Date;
class Time;
virtual bool extract(std::size_t pos, Poco::Int8& val) = 0;
/// Extracts an Int8. Returns false if null was received.
virtual bool extract(std::size_t pos, std::vector<Poco::Int8>& val);
/// Extracts an Int8 vector.
class Data_API AbstractExtractor
/// Interface used to extract data from a single result row.
/// If an extractor receives null it is not allowed to change val!
{
public:
typedef SharedPtr<AbstractExtractor> Ptr;
virtual bool extract(std::size_t pos, std::deque<Poco::Int8>& val);
/// Extracts an Int8 deque.
AbstractExtractor();
/// Creates the AbstractExtractor.
virtual bool extract(std::size_t pos, std::list<Poco::Int8>& val);
/// Extracts an Int8 list.
virtual ~AbstractExtractor();
/// Destroys the AbstractExtractor.
virtual bool extract(std::size_t pos, Poco::UInt8& val) = 0;
/// Extracts an UInt8. Returns false if null was received.
virtual bool extract(std::size_t pos, Poco::Int8 & val) = 0;
/// Extracts an Int8. Returns false if null was received.
virtual bool extract(std::size_t pos, std::vector<Poco::UInt8>& val);
/// Extracts an UInt8 vector.
virtual bool extract(std::size_t pos, std::vector<Poco::Int8> & val);
/// Extracts an Int8 vector.
virtual bool extract(std::size_t pos, std::deque<Poco::UInt8>& val);
/// Extracts an UInt8 deque.
virtual bool extract(std::size_t pos, std::deque<Poco::Int8> & val);
/// Extracts an Int8 deque.
virtual bool extract(std::size_t pos, std::list<Poco::UInt8>& val);
/// Extracts an UInt8 list.
virtual bool extract(std::size_t pos, std::list<Poco::Int8> & val);
/// Extracts an Int8 list.
virtual bool extract(std::size_t pos, Poco::Int16& val) = 0;
/// Extracts an Int16. Returns false if null was received.
virtual bool extract(std::size_t pos, Poco::UInt8 & val) = 0;
/// Extracts an UInt8. Returns false if null was received.
virtual bool extract(std::size_t pos, std::vector<Poco::Int16>& val);
/// Extracts an Int16 vector.
virtual bool extract(std::size_t pos, std::vector<Poco::UInt8> & val);
/// Extracts an UInt8 vector.
virtual bool extract(std::size_t pos, std::deque<Poco::Int16>& val);
/// Extracts an Int16 deque.
virtual bool extract(std::size_t pos, std::deque<Poco::UInt8> & val);
/// Extracts an UInt8 deque.
virtual bool extract(std::size_t pos, std::list<Poco::Int16>& val);
/// Extracts an Int16 list.
virtual bool extract(std::size_t pos, std::list<Poco::UInt8> & val);
/// Extracts an UInt8 list.
virtual bool extract(std::size_t pos, Poco::UInt16& val) = 0;
/// Extracts an UInt16. Returns false if null was received.
virtual bool extract(std::size_t pos, Poco::Int16 & val) = 0;
/// Extracts an Int16. Returns false if null was received.
virtual bool extract(std::size_t pos, std::vector<Poco::UInt16>& val);
/// Extracts an UInt16 vector.
virtual bool extract(std::size_t pos, std::vector<Poco::Int16> & val);
/// Extracts an Int16 vector.
virtual bool extract(std::size_t pos, std::deque<Poco::UInt16>& val);
/// Extracts an UInt16 deque.
virtual bool extract(std::size_t pos, std::deque<Poco::Int16> & val);
/// Extracts an Int16 deque.
virtual bool extract(std::size_t pos, std::list<Poco::UInt16>& val);
/// Extracts an UInt16 list.
virtual bool extract(std::size_t pos, std::list<Poco::Int16> & val);
/// Extracts an Int16 list.
virtual bool extract(std::size_t pos, Poco::Int32& val) = 0;
/// Extracts an Int32. Returns false if null was received.
virtual bool extract(std::size_t pos, Poco::UInt16 & val) = 0;
/// Extracts an UInt16. Returns false if null was received.
virtual bool extract(std::size_t pos, std::vector<Poco::Int32>& val);
/// Extracts an Int32 vector.
virtual bool extract(std::size_t pos, std::vector<Poco::UInt16> & val);
/// Extracts an UInt16 vector.
virtual bool extract(std::size_t pos, std::deque<Poco::Int32>& val);
/// Extracts an Int32 deque.
virtual bool extract(std::size_t pos, std::deque<Poco::UInt16> & val);
/// Extracts an UInt16 deque.
virtual bool extract(std::size_t pos, std::list<Poco::Int32>& val);
/// Extracts an Int32 list.
virtual bool extract(std::size_t pos, std::list<Poco::UInt16> & val);
/// Extracts an UInt16 list.
virtual bool extract(std::size_t pos, Poco::UInt32& val) = 0;
/// Extracts an UInt32. Returns false if null was received.
virtual bool extract(std::size_t pos, Poco::Int32 & val) = 0;
/// Extracts an Int32. Returns false if null was received.
virtual bool extract(std::size_t pos, std::vector<Poco::UInt32>& val);
/// Extracts an UInt32 vector.
virtual bool extract(std::size_t pos, std::vector<Poco::Int32> & val);
/// Extracts an Int32 vector.
virtual bool extract(std::size_t pos, std::deque<Poco::UInt32>& val);
/// Extracts an UInt32 deque.
virtual bool extract(std::size_t pos, std::deque<Poco::Int32> & val);
/// Extracts an Int32 deque.
virtual bool extract(std::size_t pos, std::list<Poco::UInt32>& val);
/// Extracts an UInt32 list.
virtual bool extract(std::size_t pos, std::list<Poco::Int32> & val);
/// Extracts an Int32 list.
virtual bool extract(std::size_t pos, Poco::Int64& val) = 0;
/// Extracts an Int64. Returns false if null was received.
virtual bool extract(std::size_t pos, Poco::UInt32 & val) = 0;
/// Extracts an UInt32. Returns false if null was received.
virtual bool extract(std::size_t pos, std::vector<Poco::Int64>& val);
/// Extracts an Int64 vector.
virtual bool extract(std::size_t pos, std::vector<Poco::UInt32> & val);
/// Extracts an UInt32 vector.
virtual bool extract(std::size_t pos, std::deque<Poco::Int64>& val);
/// Extracts an Int64 deque.
virtual bool extract(std::size_t pos, std::deque<Poco::UInt32> & val);
/// Extracts an UInt32 deque.
virtual bool extract(std::size_t pos, std::list<Poco::Int64>& val);
/// Extracts an Int64 list.
virtual bool extract(std::size_t pos, std::list<Poco::UInt32> & val);
/// Extracts an UInt32 list.
virtual bool extract(std::size_t pos, Poco::UInt64& val) = 0;
/// Extracts an UInt64. Returns false if null was received.
virtual bool extract(std::size_t pos, Poco::Int64 & val) = 0;
/// Extracts an Int64. Returns false if null was received.
virtual bool extract(std::size_t pos, std::vector<Poco::UInt64>& val);
/// Extracts an UInt64 vector.
virtual bool extract(std::size_t pos, std::vector<Poco::Int64> & val);
/// Extracts an Int64 vector.
virtual bool extract(std::size_t pos, std::deque<Poco::UInt64>& val);
/// Extracts an UInt64 deque.
virtual bool extract(std::size_t pos, std::deque<Poco::Int64> & val);
/// Extracts an Int64 deque.
virtual bool extract(std::size_t pos, std::list<Poco::UInt64>& val);
/// Extracts an UInt64 list.
virtual bool extract(std::size_t pos, std::list<Poco::Int64> & val);
/// Extracts an Int64 list.
virtual bool extract(std::size_t pos, Poco::UInt64 & val) = 0;
/// Extracts an UInt64. Returns false if null was received.
virtual bool extract(std::size_t pos, std::vector<Poco::UInt64> & val);
/// Extracts an UInt64 vector.
virtual bool extract(std::size_t pos, std::deque<Poco::UInt64> & val);
/// Extracts an UInt64 deque.
virtual bool extract(std::size_t pos, std::list<Poco::UInt64> & val);
/// Extracts an UInt64 list.
#ifndef POCO_LONG_IS_64_BIT
virtual bool extract(std::size_t pos, long& val) = 0;
/// Extracts a long. Returns false if null was received.
virtual bool extract(std::size_t pos, long & val) = 0;
/// Extracts a long. Returns false if null was received.
virtual bool extract(std::size_t pos, unsigned long& val) = 0;
/// Extracts an unsigned long. Returns false if null was received.
virtual bool extract(std::size_t pos, unsigned long & val) = 0;
/// Extracts an unsigned long. Returns false if null was received.
virtual bool extract(std::size_t pos, std::vector<long>& val);
/// Extracts a long vector.
virtual bool extract(std::size_t pos, std::vector<long> & val);
/// Extracts a long vector.
virtual bool extract(std::size_t pos, std::deque<long>& val);
/// Extracts a long deque.
virtual bool extract(std::size_t pos, std::deque<long> & val);
/// Extracts a long deque.
virtual bool extract(std::size_t pos, std::list<long>& val);
/// Extracts a long list.
virtual bool extract(std::size_t pos, std::list<long> & val);
/// Extracts a long list.
#endif
virtual bool extract(std::size_t pos, bool& val) = 0;
/// Extracts a boolean. Returns false if null was received.
virtual bool extract(std::size_t pos, bool & val) = 0;
/// Extracts a boolean. Returns false if null was received.
virtual bool extract(std::size_t pos, std::vector<bool>& val);
/// Extracts a boolean vector.
virtual bool extract(std::size_t pos, std::vector<bool> & val);
/// Extracts a boolean vector.
virtual bool extract(std::size_t pos, std::deque<bool>& val);
/// Extracts a boolean deque.
virtual bool extract(std::size_t pos, std::deque<bool> & val);
/// Extracts a boolean deque.
virtual bool extract(std::size_t pos, std::list<bool>& val);
/// Extracts a boolean list.
virtual bool extract(std::size_t pos, std::list<bool> & val);
/// Extracts a boolean list.
virtual bool extract(std::size_t pos, float& val) = 0;
/// Extracts a float. Returns false if null was received.
virtual bool extract(std::size_t pos, float & val) = 0;
/// Extracts a float. Returns false if null was received.
virtual bool extract(std::size_t pos, std::vector<float>& val);
/// Extracts a float vector.
virtual bool extract(std::size_t pos, std::vector<float> & val);
/// Extracts a float vector.
virtual bool extract(std::size_t pos, std::deque<float>& val);
/// Extracts a float deque.
virtual bool extract(std::size_t pos, std::deque<float> & val);
/// Extracts a float deque.
virtual bool extract(std::size_t pos, std::list<float>& val);
/// Extracts a float list.
virtual bool extract(std::size_t pos, std::list<float> & val);
/// Extracts a float list.
virtual bool extract(std::size_t pos, double& val) = 0;
/// Extracts a double. Returns false if null was received.
virtual bool extract(std::size_t pos, double & val) = 0;
/// Extracts a double. Returns false if null was received.
virtual bool extract(std::size_t pos, std::vector<double>& val);
/// Extracts a double vector.
virtual bool extract(std::size_t pos, std::vector<double> & val);
/// Extracts a double vector.
virtual bool extract(std::size_t pos, std::deque<double>& val);
/// Extracts a double deque.
virtual bool extract(std::size_t pos, std::deque<double> & val);
/// Extracts a double deque.
virtual bool extract(std::size_t pos, std::list<double>& val);
/// Extracts a double list.
virtual bool extract(std::size_t pos, std::list<double> & val);
/// Extracts a double list.
virtual bool extract(std::size_t pos, char& val) = 0;
/// Extracts a single character. Returns false if null was received.
virtual bool extract(std::size_t pos, char & val) = 0;
/// Extracts a single character. Returns false if null was received.
virtual bool extract(std::size_t pos, std::vector<char>& val);
/// Extracts a character vector.
virtual bool extract(std::size_t pos, std::vector<char> & val);
/// Extracts a character vector.
virtual bool extract(std::size_t pos, std::deque<char>& val);
/// Extracts a character deque.
virtual bool extract(std::size_t pos, std::deque<char> & val);
/// Extracts a character deque.
virtual bool extract(std::size_t pos, std::list<char>& val);
/// Extracts a character list.
virtual bool extract(std::size_t pos, std::list<char> & val);
/// Extracts a character list.
virtual bool extract(std::size_t pos, std::string& val) = 0;
/// Extracts a string. Returns false if null was received.
virtual bool extract(std::size_t pos, std::string & val) = 0;
/// Extracts a string. Returns false if null was received.
virtual bool extract(std::size_t pos, std::vector<std::string>& val);
/// Extracts a string vector.
virtual bool extract(std::size_t pos, std::vector<std::string> & val);
/// Extracts a string vector.
virtual bool extract(std::size_t pos, std::deque<std::string>& val);
/// Extracts a string deque.
virtual bool extract(std::size_t pos, std::deque<std::string> & val);
/// Extracts a string deque.
virtual bool extract(std::size_t pos, std::list<std::string>& val);
/// Extracts a string list.
virtual bool extract(std::size_t pos, std::list<std::string> & val);
/// Extracts a string list.
virtual bool extract(std::size_t pos, UTF16String& val);
/// Extracts a UTF16String. Returns false if null was received.
virtual bool extract(std::size_t pos, UTF16String & val);
/// Extracts a UTF16String. Returns false if null was received.
virtual bool extract(std::size_t pos, std::vector<UTF16String>& val);
/// Extracts a UTF16String vector.
virtual bool extract(std::size_t pos, std::vector<UTF16String> & val);
/// Extracts a UTF16String vector.
virtual bool extract(std::size_t pos, std::deque<UTF16String>& val);
/// Extracts a UTF16String deque.
virtual bool extract(std::size_t pos, std::deque<UTF16String> & val);
/// Extracts a UTF16String deque.
virtual bool extract(std::size_t pos, std::list<UTF16String>& val);
/// Extracts a UTF16String list.
virtual bool extract(std::size_t pos, std::list<UTF16String> & val);
/// Extracts a UTF16String list.
virtual bool extract(std::size_t pos, BLOB& val) = 0;
/// Extracts a BLOB. Returns false if null was received.
virtual bool extract(std::size_t pos, BLOB & val) = 0;
/// Extracts a BLOB. Returns false if null was received.
virtual bool extract(std::size_t pos, CLOB& val) = 0;
/// Extracts a CLOB. Returns false if null was received.
virtual bool extract(std::size_t pos, CLOB & val) = 0;
/// Extracts a CLOB. Returns false if null was received.
virtual bool extract(std::size_t pos, std::vector<BLOB>& val);
/// Extracts a BLOB vector.
virtual bool extract(std::size_t pos, std::vector<BLOB> & val);
/// Extracts a BLOB vector.
virtual bool extract(std::size_t pos, std::deque<BLOB>& val);
/// Extracts a BLOB deque.
virtual bool extract(std::size_t pos, std::deque<BLOB> & val);
/// Extracts a BLOB deque.
virtual bool extract(std::size_t pos, std::list<BLOB>& val);
/// Extracts a BLOB list.
virtual bool extract(std::size_t pos, std::list<BLOB> & val);
/// Extracts a BLOB list.
virtual bool extract(std::size_t pos, std::vector<CLOB>& val);
/// Extracts a CLOB vector.
virtual bool extract(std::size_t pos, std::vector<CLOB> & val);
/// Extracts a CLOB vector.
virtual bool extract(std::size_t pos, std::deque<CLOB>& val);
/// Extracts a CLOB deque.
virtual bool extract(std::size_t pos, std::deque<CLOB> & val);
/// Extracts a CLOB deque.
virtual bool extract(std::size_t pos, std::list<CLOB>& val);
/// Extracts a CLOB list.
virtual bool extract(std::size_t pos, std::list<CLOB> & val);
/// Extracts a CLOB list.
virtual bool extract(std::size_t pos, DateTime& val) = 0;
/// Extracts a DateTime. Returns false if null was received.
virtual bool extract(std::size_t pos, DateTime & val) = 0;
/// Extracts a DateTime. Returns false if null was received.
virtual bool extract(std::size_t pos, std::vector<DateTime>& val);
/// Extracts a DateTime vector.
virtual bool extract(std::size_t pos, std::vector<DateTime> & val);
/// Extracts a DateTime vector.
virtual bool extract(std::size_t pos, std::deque<DateTime>& val);
/// Extracts a DateTime deque.
virtual bool extract(std::size_t pos, std::deque<DateTime> & val);
/// Extracts a DateTime deque.
virtual bool extract(std::size_t pos, std::list<DateTime>& val);
/// Extracts a DateTime list.
virtual bool extract(std::size_t pos, std::list<DateTime> & val);
/// Extracts a DateTime list.
virtual bool extract(std::size_t pos, Date& val) = 0;
/// Extracts a Date. Returns false if null was received.
virtual bool extract(std::size_t pos, Date & val) = 0;
/// Extracts a Date. Returns false if null was received.
virtual bool extract(std::size_t pos, std::vector<Date>& val);
/// Extracts a Date vector.
virtual bool extract(std::size_t pos, std::vector<Date> & val);
/// Extracts a Date vector.
virtual bool extract(std::size_t pos, std::deque<Date>& val);
/// Extracts a Date deque.
virtual bool extract(std::size_t pos, std::deque<Date> & val);
/// Extracts a Date deque.
virtual bool extract(std::size_t pos, std::list<Date>& val);
/// Extracts a Date list.
virtual bool extract(std::size_t pos, std::list<Date> & val);
/// Extracts a Date list.
virtual bool extract(std::size_t pos, Time& val) = 0;
/// Extracts a Time. Returns false if null was received.
virtual bool extract(std::size_t pos, Time & val) = 0;
/// Extracts a Time. Returns false if null was received.
virtual bool extract(std::size_t pos, std::vector<Time>& val);
/// Extracts a Time vector.
virtual bool extract(std::size_t pos, std::vector<Time> & val);
/// Extracts a Time vector.
virtual bool extract(std::size_t pos, std::deque<Time>& val);
/// Extracts a Time deque.
virtual bool extract(std::size_t pos, std::deque<Time> & val);
/// Extracts a Time deque.
virtual bool extract(std::size_t pos, std::list<Time>& val);
/// Extracts a Time list.
virtual bool extract(std::size_t pos, std::list<Time> & val);
/// Extracts a Time list.
virtual bool extract(std::size_t pos, Any& val) = 0;
/// Extracts an Any. Returns false if null was received.
virtual bool extract(std::size_t pos, Any & val) = 0;
/// Extracts an Any. Returns false if null was received.
virtual bool extract(std::size_t pos, std::vector<Any>& val);
/// Extracts an Any vector.
virtual bool extract(std::size_t pos, std::vector<Any> & val);
/// Extracts an Any vector.
virtual bool extract(std::size_t pos, std::deque<Any>& val);
/// Extracts an Any deque.
virtual bool extract(std::size_t pos, std::deque<Any> & val);
/// Extracts an Any deque.
virtual bool extract(std::size_t pos, std::list<Any>& val);
/// Extracts an Any list.
virtual bool extract(std::size_t pos, std::list<Any> & val);
/// Extracts an Any list.
virtual bool extract(std::size_t pos, Poco::Dynamic::Var& val) = 0;
/// Extracts a Var. Returns false if null was received.
virtual bool extract(std::size_t pos, Poco::Dynamic::Var & val) = 0;
/// Extracts a Var. Returns false if null was received.
virtual bool extract(std::size_t pos, std::vector<Poco::Dynamic::Var>& val);
/// Extracts a Var vector.
virtual bool extract(std::size_t pos, std::vector<Poco::Dynamic::Var> & val);
/// Extracts a Var vector.
virtual bool extract(std::size_t pos, std::deque<Poco::Dynamic::Var>& val);
/// Extracts a Var deque.
virtual bool extract(std::size_t pos, std::deque<Poco::Dynamic::Var> & val);
/// Extracts a Var deque.
virtual bool extract(std::size_t pos, std::list<Poco::Dynamic::Var>& val);
/// Extracts a Var list.
virtual bool extract(std::size_t pos, std::list<Poco::Dynamic::Var> & val);
/// Extracts a Var list.
virtual bool isNull(std::size_t col, std::size_t row = POCO_DATA_INVALID_ROW) = 0;
/// Returns true if the value at [col,row] position is null.
virtual bool isNull(std::size_t col, std::size_t row = POCO_DATA_INVALID_ROW) = 0;
/// Returns true if the value at [col,row] position is null.
virtual void reset();
/// Resets any information internally cached by the extractor.
};
virtual void reset();
/// Resets any information internally cached by the extractor.
};
///
/// inlines
///
inline void AbstractExtractor::reset()
{
//default no-op
}
///
/// inlines
///
inline void AbstractExtractor::reset()
{
//default no-op
}
} } // namespace Poco::Data
} // namespace Poco::Data
#endif // Data_AbstractExtractor_INCLUDED

View File

@ -18,54 +18,57 @@
#define Data_AbstractPreparation_INCLUDED
#include "Poco/Data/Data.h"
#include "Poco/Data/AbstractPreparator.h"
#include "Poco/SharedPtr.h"
#include <cstddef>
#include "Poco/Data/AbstractPreparator.h"
#include "Poco/Data/Data.h"
#include "Poco/SharedPtr.h"
namespace Poco {
namespace Data {
class Data_API AbstractPreparation
/// Interface for calling the appropriate AbstractPreparator method
namespace Poco
{
public:
typedef SharedPtr<AbstractPreparation> Ptr;
typedef AbstractPreparator::Ptr PreparatorPtr;
AbstractPreparation(PreparatorPtr pPreparator);
/// Creates the AbstractPreparation.
virtual ~AbstractPreparation();
/// Destroys the AbstractPreparation.
virtual void prepare() = 0;
/// Prepares data.
protected:
AbstractPreparation();
AbstractPreparation(const AbstractPreparation&);
AbstractPreparation& operator = (const AbstractPreparation&);
PreparatorPtr preparation();
/// Returns the preparation object
PreparatorPtr _pPreparator;
};
//
// inlines
//
inline AbstractPreparation::PreparatorPtr AbstractPreparation::preparation()
namespace Data
{
return _pPreparator;
class Data_API AbstractPreparation
/// Interface for calling the appropriate AbstractPreparator method
{
public:
typedef SharedPtr<AbstractPreparation> Ptr;
typedef AbstractPreparator::Ptr PreparatorPtr;
AbstractPreparation(PreparatorPtr pPreparator);
/// Creates the AbstractPreparation.
virtual ~AbstractPreparation();
/// Destroys the AbstractPreparation.
virtual void prepare() = 0;
/// Prepares data.
protected:
AbstractPreparation();
AbstractPreparation(const AbstractPreparation &);
AbstractPreparation & operator=(const AbstractPreparation &);
PreparatorPtr preparation();
/// Returns the preparation object
PreparatorPtr _pPreparator;
};
//
// inlines
//
inline AbstractPreparation::PreparatorPtr AbstractPreparation::preparation()
{
return _pPreparator;
}
}
} } // namespace Poco::Data
} // namespace Poco::Data
#endif // Data_AbstractPreparation_INCLUDED

View File

@ -18,371 +18,375 @@
#define Data_AbstractPreparator_INCLUDED
#include "Poco/Data/Data.h"
#include "Poco/RefCountedObject.h"
#include "Poco/Data/LOB.h"
#include "Poco/UTFString.h"
#include <vector>
#include <cstddef>
#include <deque>
#include <list>
#include <cstddef>
#include <vector>
#include "Poco/Data/Data.h"
#include "Poco/Data/LOB.h"
#include "Poco/RefCountedObject.h"
#include "Poco/UTFString.h"
namespace Poco {
namespace Poco
{
class DateTime;
class Any;
namespace Dynamic {
class Var;
namespace Dynamic
{
class Var;
}
namespace Data {
class Date;
class Time;
class Data_API AbstractPreparator
/// Interface used for database preparation where we first have to register all data types
/// (and memory output locations) before extracting data, e.g. ODBC.
/// Extract works as two-phase extract: first we call prepare once, then extract n-times.
/// There are cases (bulk operations using std::vector storage) when extract is called only once.
/// The value passed to a prepare() call is not used by the prepare, serving only as an indication
/// of the data type being prepared, thus all values are passed as const references.
/// Implementing this interface is not mandatory for a connector. Connectors that only extract data
/// after SQL execution (e.g. SQLite) do not need this functionality at all.
namespace Data
{
public:
typedef SharedPtr<AbstractPreparator> Ptr;
AbstractPreparator(Poco::UInt32 length = 1u);
/// Creates the AbstractPreparator.
virtual ~AbstractPreparator();
/// Destroys the AbstractPreparator.
class Date;
class Time;
virtual void prepare(std::size_t pos, const Poco::Int8&) = 0;
/// Prepares an Int8.
virtual void prepare(std::size_t pos, const std::vector<Poco::Int8>& val);
/// Prepares an Int8 vector.
class Data_API AbstractPreparator
/// Interface used for database preparation where we first have to register all data types
/// (and memory output locations) before extracting data, e.g. ODBC.
/// Extract works as two-phase extract: first we call prepare once, then extract n-times.
/// There are cases (bulk operations using std::vector storage) when extract is called only once.
/// The value passed to a prepare() call is not used by the prepare, serving only as an indication
/// of the data type being prepared, thus all values are passed as const references.
/// Implementing this interface is not mandatory for a connector. Connectors that only extract data
/// after SQL execution (e.g. SQLite) do not need this functionality at all.
{
public:
typedef SharedPtr<AbstractPreparator> Ptr;
virtual void prepare(std::size_t pos, const std::deque<Poco::Int8>& val);
/// Prepares an Int8 deque.
AbstractPreparator(Poco::UInt32 length = 1u);
/// Creates the AbstractPreparator.
virtual void prepare(std::size_t pos, const std::list<Poco::Int8>& val);
/// Prepares an Int8 list.
virtual ~AbstractPreparator();
/// Destroys the AbstractPreparator.
virtual void prepare(std::size_t pos, const Poco::UInt8&) = 0;
/// Prepares an UInt8.
virtual void prepare(std::size_t pos, const Poco::Int8 &) = 0;
/// Prepares an Int8.
virtual void prepare(std::size_t pos, const std::vector<Poco::UInt8>& val);
/// Prepares an UInt8 vector.
virtual void prepare(std::size_t pos, const std::vector<Poco::Int8> & val);
/// Prepares an Int8 vector.
virtual void prepare(std::size_t pos, const std::deque<Poco::UInt8>& val);
/// Prepares an UInt8 deque.
virtual void prepare(std::size_t pos, const std::deque<Poco::Int8> & val);
/// Prepares an Int8 deque.
virtual void prepare(std::size_t pos, const std::list<Poco::UInt8>& val);
/// Prepares an UInt8 list.
virtual void prepare(std::size_t pos, const std::list<Poco::Int8> & val);
/// Prepares an Int8 list.
virtual void prepare(std::size_t pos, const Poco::Int16&) = 0;
/// Prepares an Int16.
virtual void prepare(std::size_t pos, const Poco::UInt8 &) = 0;
/// Prepares an UInt8.
virtual void prepare(std::size_t pos, const std::vector<Poco::Int16>& val);
/// Prepares an Int16 vector.
virtual void prepare(std::size_t pos, const std::vector<Poco::UInt8> & val);
/// Prepares an UInt8 vector.
virtual void prepare(std::size_t pos, const std::deque<Poco::Int16>& val);
/// Prepares an Int16 deque.
virtual void prepare(std::size_t pos, const std::deque<Poco::UInt8> & val);
/// Prepares an UInt8 deque.
virtual void prepare(std::size_t pos, const std::list<Poco::Int16>& val);
/// Prepares an Int16 list.
virtual void prepare(std::size_t pos, const std::list<Poco::UInt8> & val);
/// Prepares an UInt8 list.
virtual void prepare(std::size_t pos, const Poco::UInt16&) = 0;
/// Prepares an UInt16.
virtual void prepare(std::size_t pos, const Poco::Int16 &) = 0;
/// Prepares an Int16.
virtual void prepare(std::size_t pos, const std::vector<Poco::UInt16>& val);
/// Prepares an UInt16 vector.
virtual void prepare(std::size_t pos, const std::vector<Poco::Int16> & val);
/// Prepares an Int16 vector.
virtual void prepare(std::size_t pos, const std::deque<Poco::UInt16>& val);
/// Prepares an UInt16 deque.
virtual void prepare(std::size_t pos, const std::deque<Poco::Int16> & val);
/// Prepares an Int16 deque.
virtual void prepare(std::size_t pos, const std::list<Poco::UInt16>& val);
/// Prepares an UInt16 list.
virtual void prepare(std::size_t pos, const std::list<Poco::Int16> & val);
/// Prepares an Int16 list.
virtual void prepare(std::size_t pos, const Poco::Int32&) = 0;
/// Prepares an Int32.
virtual void prepare(std::size_t pos, const Poco::UInt16 &) = 0;
/// Prepares an UInt16.
virtual void prepare(std::size_t pos, const std::vector<Poco::Int32>& val);
/// Prepares an Int32 vector.
virtual void prepare(std::size_t pos, const std::vector<Poco::UInt16> & val);
/// Prepares an UInt16 vector.
virtual void prepare(std::size_t pos, const std::deque<Poco::Int32>& val);
/// Prepares an Int32 deque.
virtual void prepare(std::size_t pos, const std::deque<Poco::UInt16> & val);
/// Prepares an UInt16 deque.
virtual void prepare(std::size_t pos, const std::list<Poco::Int32>& val);
/// Prepares an Int32 list.
virtual void prepare(std::size_t pos, const std::list<Poco::UInt16> & val);
/// Prepares an UInt16 list.
virtual void prepare(std::size_t pos, const Poco::UInt32&) = 0;
/// Prepares an UInt32.
virtual void prepare(std::size_t pos, const Poco::Int32 &) = 0;
/// Prepares an Int32.
virtual void prepare(std::size_t pos, const std::vector<Poco::UInt32>& val);
/// Prepares an UInt32 vector.
virtual void prepare(std::size_t pos, const std::vector<Poco::Int32> & val);
/// Prepares an Int32 vector.
virtual void prepare(std::size_t pos, const std::deque<Poco::UInt32>& val);
/// Prepares an UInt32 deque.
virtual void prepare(std::size_t pos, const std::deque<Poco::Int32> & val);
/// Prepares an Int32 deque.
virtual void prepare(std::size_t pos, const std::list<Poco::UInt32>& val);
/// Prepares an UInt32 list.
virtual void prepare(std::size_t pos, const std::list<Poco::Int32> & val);
/// Prepares an Int32 list.
virtual void prepare(std::size_t pos, const Poco::Int64&) = 0;
/// Prepares an Int64.
virtual void prepare(std::size_t pos, const Poco::UInt32 &) = 0;
/// Prepares an UInt32.
virtual void prepare(std::size_t pos, const std::vector<Poco::Int64>& val);
/// Prepares an Int64 vector.
virtual void prepare(std::size_t pos, const std::vector<Poco::UInt32> & val);
/// Prepares an UInt32 vector.
virtual void prepare(std::size_t pos, const std::deque<Poco::Int64>& val);
/// Prepares an Int64 deque.
virtual void prepare(std::size_t pos, const std::deque<Poco::UInt32> & val);
/// Prepares an UInt32 deque.
virtual void prepare(std::size_t pos, const std::list<Poco::Int64>& val);
/// Prepares an Int64 list.
virtual void prepare(std::size_t pos, const std::list<Poco::UInt32> & val);
/// Prepares an UInt32 list.
virtual void prepare(std::size_t pos, const Poco::UInt64&) = 0;
/// Prepares an UInt64.
virtual void prepare(std::size_t pos, const Poco::Int64 &) = 0;
/// Prepares an Int64.
virtual void prepare(std::size_t pos, const std::vector<Poco::UInt64>& val);
/// Prepares an UInt64 vector.
virtual void prepare(std::size_t pos, const std::vector<Poco::Int64> & val);
/// Prepares an Int64 vector.
virtual void prepare(std::size_t pos, const std::deque<Poco::UInt64>& val);
/// Prepares an UInt64 deque.
virtual void prepare(std::size_t pos, const std::deque<Poco::Int64> & val);
/// Prepares an Int64 deque.
virtual void prepare(std::size_t pos, const std::list<Poco::UInt64>& val);
/// Prepares an UInt64 list.
virtual void prepare(std::size_t pos, const std::list<Poco::Int64> & val);
/// Prepares an Int64 list.
virtual void prepare(std::size_t pos, const Poco::UInt64 &) = 0;
/// Prepares an UInt64.
virtual void prepare(std::size_t pos, const std::vector<Poco::UInt64> & val);
/// Prepares an UInt64 vector.
virtual void prepare(std::size_t pos, const std::deque<Poco::UInt64> & val);
/// Prepares an UInt64 deque.
virtual void prepare(std::size_t pos, const std::list<Poco::UInt64> & val);
/// Prepares an UInt64 list.
#ifndef POCO_LONG_IS_64_BIT
virtual void prepare(std::size_t pos, const long&) = 0;
/// Prepares a long.
virtual void prepare(std::size_t pos, const long &) = 0;
/// Prepares a long.
virtual void prepare(std::size_t pos, const unsigned long&) = 0;
/// Prepares an unsigned long.
virtual void prepare(std::size_t pos, const unsigned long &) = 0;
/// Prepares an unsigned long.
virtual void prepare(std::size_t pos, const std::vector<long>& val);
/// Prepares a long vector.
virtual void prepare(std::size_t pos, const std::vector<long> & val);
/// Prepares a long vector.
virtual void prepare(std::size_t pos, const std::deque<long>& val);
/// Prepares a long deque.
virtual void prepare(std::size_t pos, const std::deque<long> & val);
/// Prepares a long deque.
virtual void prepare(std::size_t pos, const std::list<long>& val);
/// Prepares a long list.
virtual void prepare(std::size_t pos, const std::list<long> & val);
/// Prepares a long list.
#endif
virtual void prepare(std::size_t pos, const bool&) = 0;
/// Prepares a boolean.
virtual void prepare(std::size_t pos, const bool &) = 0;
/// Prepares a boolean.
virtual void prepare(std::size_t pos, const std::vector<bool>& val);
/// Prepares a boolean vector.
virtual void prepare(std::size_t pos, const std::vector<bool> & val);
/// Prepares a boolean vector.
virtual void prepare(std::size_t pos, const std::deque<bool>& val);
/// Prepares a boolean deque.
virtual void prepare(std::size_t pos, const std::deque<bool> & val);
/// Prepares a boolean deque.
virtual void prepare(std::size_t pos, const std::list<bool>& val);
/// Prepares a boolean list.
virtual void prepare(std::size_t pos, const std::list<bool> & val);
/// Prepares a boolean list.
virtual void prepare(std::size_t pos, const float&) = 0;
/// Prepares a float.
virtual void prepare(std::size_t pos, const float &) = 0;
/// Prepares a float.
virtual void prepare(std::size_t pos, const std::vector<float>& val);
/// Prepares a float vector.
virtual void prepare(std::size_t pos, const std::vector<float> & val);
/// Prepares a float vector.
virtual void prepare(std::size_t pos, const std::deque<float>& val);
/// Prepares a float deque.
virtual void prepare(std::size_t pos, const std::deque<float> & val);
/// Prepares a float deque.
virtual void prepare(std::size_t pos, const std::list<float>& val);
/// Prepares a float list.
virtual void prepare(std::size_t pos, const std::list<float> & val);
/// Prepares a float list.
virtual void prepare(std::size_t pos, const double&) = 0;
/// Prepares a double.
virtual void prepare(std::size_t pos, const double &) = 0;
/// Prepares a double.
virtual void prepare(std::size_t pos, const std::vector<double>& val);
/// Prepares a double vector.
virtual void prepare(std::size_t pos, const std::vector<double> & val);
/// Prepares a double vector.
virtual void prepare(std::size_t pos, const std::deque<double>& val);
/// Prepares a double deque.
virtual void prepare(std::size_t pos, const std::deque<double> & val);
/// Prepares a double deque.
virtual void prepare(std::size_t pos, const std::list<double>& val);
/// Prepares a double list.
virtual void prepare(std::size_t pos, const std::list<double> & val);
/// Prepares a double list.
virtual void prepare(std::size_t pos, const char&) = 0;
/// Prepares a single character.
virtual void prepare(std::size_t pos, const char &) = 0;
/// Prepares a single character.
virtual void prepare(std::size_t pos, const std::vector<char>& val);
/// Prepares a character vector.
virtual void prepare(std::size_t pos, const std::vector<char> & val);
/// Prepares a character vector.
virtual void prepare(std::size_t pos, const std::deque<char>& val);
/// Prepares a character deque.
virtual void prepare(std::size_t pos, const std::deque<char> & val);
/// Prepares a character deque.
virtual void prepare(std::size_t pos, const std::list<char>& val);
/// Prepares a character list.
virtual void prepare(std::size_t pos, const std::list<char> & val);
/// Prepares a character list.
virtual void prepare(std::size_t pos, const std::string&) = 0;
/// Prepares a string.
virtual void prepare(std::size_t pos, const std::string &) = 0;
/// Prepares a string.
virtual void prepare(std::size_t pos, const std::vector<std::string>& val);
/// Prepares a string vector.
virtual void prepare(std::size_t pos, const std::vector<std::string> & val);
/// Prepares a string vector.
virtual void prepare(std::size_t pos, const std::deque<std::string>& val);
/// Prepares a string deque.
virtual void prepare(std::size_t pos, const std::deque<std::string> & val);
/// Prepares a string deque.
virtual void prepare(std::size_t pos, const std::list<std::string>& val);
/// Prepares a character list.
virtual void prepare(std::size_t pos, const std::list<std::string> & val);
/// Prepares a character list.
virtual void prepare(std::size_t pos, const UTF16String&);
/// Prepares a UTF16String.
virtual void prepare(std::size_t pos, const UTF16String &);
/// Prepares a UTF16String.
virtual void prepare(std::size_t pos, const std::vector<UTF16String>& val);
/// Prepares a UTF16String vector.
virtual void prepare(std::size_t pos, const std::vector<UTF16String> & val);
/// Prepares a UTF16String vector.
virtual void prepare(std::size_t pos, const std::deque<UTF16String>& val);
/// Prepares a UTF16String deque.
virtual void prepare(std::size_t pos, const std::deque<UTF16String> & val);
/// Prepares a UTF16String deque.
virtual void prepare(std::size_t pos, const std::list<UTF16String>& val);
/// Prepares a UTF16String list.
virtual void prepare(std::size_t pos, const std::list<UTF16String> & val);
/// Prepares a UTF16String list.
virtual void prepare(std::size_t pos, const BLOB&) = 0;
/// Prepares a BLOB.
virtual void prepare(std::size_t pos, const BLOB &) = 0;
/// Prepares a BLOB.
virtual void prepare(std::size_t pos, const CLOB&) = 0;
/// Prepares a CLOB.
virtual void prepare(std::size_t pos, const CLOB &) = 0;
/// Prepares a CLOB.
virtual void prepare(std::size_t pos, const std::vector<BLOB>& val);
/// Prepares a BLOB vector.
virtual void prepare(std::size_t pos, const std::vector<BLOB> & val);
/// Prepares a BLOB vector.
virtual void prepare(std::size_t pos, const std::deque<BLOB>& val);
/// Prepares a BLOB deque.
virtual void prepare(std::size_t pos, const std::deque<BLOB> & val);
/// Prepares a BLOB deque.
virtual void prepare(std::size_t pos, const std::list<BLOB>& val);
/// Prepares a BLOB list.
virtual void prepare(std::size_t pos, const std::list<BLOB> & val);
/// Prepares a BLOB list.
virtual void prepare(std::size_t pos, const std::vector<CLOB>& val);
/// Prepares a CLOB vector.
virtual void prepare(std::size_t pos, const std::vector<CLOB> & val);
/// Prepares a CLOB vector.
virtual void prepare(std::size_t pos, const std::deque<CLOB>& val);
/// Prepares a CLOB deque.
virtual void prepare(std::size_t pos, const std::deque<CLOB> & val);
/// Prepares a CLOB deque.
virtual void prepare(std::size_t pos, const std::list<CLOB>& val);
/// Prepares a CLOB list.
virtual void prepare(std::size_t pos, const std::list<CLOB> & val);
/// Prepares a CLOB list.
virtual void prepare(std::size_t pos, const DateTime&) = 0;
/// Prepares a DateTime.
virtual void prepare(std::size_t pos, const DateTime &) = 0;
/// Prepares a DateTime.
virtual void prepare(std::size_t pos, const std::vector<DateTime>& val);
/// Prepares a DateTime vector.
virtual void prepare(std::size_t pos, const std::vector<DateTime> & val);
/// Prepares a DateTime vector.
virtual void prepare(std::size_t pos, const std::deque<DateTime>& val);
/// Prepares a DateTime deque.
virtual void prepare(std::size_t pos, const std::deque<DateTime> & val);
/// Prepares a DateTime deque.
virtual void prepare(std::size_t pos, const std::list<DateTime>& val);
/// Prepares a DateTime list.
virtual void prepare(std::size_t pos, const std::list<DateTime> & val);
/// Prepares a DateTime list.
virtual void prepare(std::size_t pos, const Date&) = 0;
/// Prepares a Date.
virtual void prepare(std::size_t pos, const Date &) = 0;
/// Prepares a Date.
virtual void prepare(std::size_t pos, const std::vector<Date>& val);
/// Prepares a Date vector.
virtual void prepare(std::size_t pos, const std::vector<Date> & val);
/// Prepares a Date vector.
virtual void prepare(std::size_t pos, const std::deque<Date>& val);
/// Prepares a Date deque.
virtual void prepare(std::size_t pos, const std::deque<Date> & val);
/// Prepares a Date deque.
virtual void prepare(std::size_t pos, const std::list<Date>& val);
/// Prepares a Date list.
virtual void prepare(std::size_t pos, const std::list<Date> & val);
/// Prepares a Date list.
virtual void prepare(std::size_t pos, const Time&) = 0;
/// Prepares a Time.
virtual void prepare(std::size_t pos, const Time &) = 0;
/// Prepares a Time.
virtual void prepare(std::size_t pos, const std::vector<Time>& val);
/// Prepares a Time vector.
virtual void prepare(std::size_t pos, const std::vector<Time> & val);
/// Prepares a Time vector.
virtual void prepare(std::size_t pos, const std::deque<Time>& val);
/// Prepares a Time deque.
virtual void prepare(std::size_t pos, const std::list<Time>& val);
/// Prepares a Time list.
virtual void prepare(std::size_t pos, const std::deque<Time> & val);
/// Prepares a Time deque.
virtual void prepare(std::size_t pos, const Any&) = 0;
/// Prepares an Any.
virtual void prepare(std::size_t pos, const std::list<Time> & val);
/// Prepares a Time list.
virtual void prepare(std::size_t pos, const std::vector<Any>& val);
/// Prepares an Any vector.
virtual void prepare(std::size_t pos, const Any &) = 0;
/// Prepares an Any.
virtual void prepare(std::size_t pos, const std::deque<Any>& val);
/// Prepares an Any deque.
virtual void prepare(std::size_t pos, const std::vector<Any> & val);
/// Prepares an Any vector.
virtual void prepare(std::size_t pos, const std::list<Any>& val);
/// Prepares an Any list.
virtual void prepare(std::size_t pos, const std::deque<Any> & val);
/// Prepares an Any deque.
virtual void prepare(std::size_t pos, const Poco::Dynamic::Var&) = 0;
/// Prepares a Var.
virtual void prepare(std::size_t pos, const std::list<Any> & val);
/// Prepares an Any list.
virtual void prepare(std::size_t pos, const std::vector<Poco::Dynamic::Var>& val);
/// Prepares a Var vector.
virtual void prepare(std::size_t pos, const Poco::Dynamic::Var &) = 0;
/// Prepares a Var.
virtual void prepare(std::size_t pos, const std::deque<Poco::Dynamic::Var>& val);
/// Prepares a Var deque.
virtual void prepare(std::size_t pos, const std::vector<Poco::Dynamic::Var> & val);
/// Prepares a Var vector.
virtual void prepare(std::size_t pos, const std::list<Poco::Dynamic::Var>& val);
/// Prepares a Var list.
virtual void prepare(std::size_t pos, const std::deque<Poco::Dynamic::Var> & val);
/// Prepares a Var deque.
void setLength(Poco::UInt32 length);
/// Sets the length of prepared data.
/// Needed only for data lengths greater than 1 (i.e. for
/// bulk operations).
virtual void prepare(std::size_t pos, const std::list<Poco::Dynamic::Var> & val);
/// Prepares a Var list.
Poco::UInt32 getLength() const;
/// Returns the length of prepared data. Defaults to 1.
/// The length is greater than one for bulk operations.
void setLength(Poco::UInt32 length);
/// Sets the length of prepared data.
/// Needed only for data lengths greater than 1 (i.e. for
/// bulk operations).
void setBulk(bool bulkPrep = true);
/// Sets bulk operation flag (always false at object creation time)
Poco::UInt32 getLength() const;
/// Returns the length of prepared data. Defaults to 1.
/// The length is greater than one for bulk operations.
bool isBulk() const;
/// Returns bulk operation flag.
void setBulk(bool bulkPrep = true);
/// Sets bulk operation flag (always false at object creation time)
private:
Poco::UInt32 _length;
bool _bulk;
};
bool isBulk() const;
/// Returns bulk operation flag.
private:
Poco::UInt32 _length;
bool _bulk;
};
///
/// inlines
///
inline void AbstractPreparator::setLength(Poco::UInt32 length)
{
_length = length;
}
inline Poco::UInt32 AbstractPreparator::getLength() const
{
return _length;
}
inline void AbstractPreparator::setBulk(bool bulkPrep)
{
_bulk = bulkPrep;
}
inline bool AbstractPreparator::isBulk() const
{
return _bulk;
}
///
/// inlines
///
inline void AbstractPreparator::setLength(Poco::UInt32 length)
{
_length = length;
}
inline Poco::UInt32 AbstractPreparator::getLength() const
{
return _length;
}
inline void AbstractPreparator::setBulk(bool bulkPrep)
{
_bulk = bulkPrep;
}
inline bool AbstractPreparator::isBulk() const
{
return _bulk;
}
} } // namespace Poco::Data
} // namespace Poco::Data
#endif // Data_AbstractPreparator_INCLUDED

View File

@ -18,298 +18,295 @@
#define Data_AbstractSessionImpl_INCLUDED
#include "Poco/Data/Data.h"
#include "Poco/Data/SessionImpl.h"
#include "Poco/Data/DataException.h"
#include <map>
#include "Poco/Data/Data.h"
#include "Poco/Data/DataException.h"
#include "Poco/Data/SessionImpl.h"
namespace Poco {
namespace Data {
template <class C>
class AbstractSessionImpl: public SessionImpl
/// A partial implementation of SessionImpl, providing
/// features and properties management.
///
/// To implement a certain feature or property, a subclass
/// must provide setter and getter methods and register
/// them with addFeature() or addProperty().
namespace Poco
{
namespace Data
{
public:
typedef void (C::*FeatureSetter)(const std::string&, bool);
/// The setter method for a feature.
typedef bool (C::*FeatureGetter)(const std::string&);
/// The getter method for a feature.
typedef void (C::*PropertySetter)(const std::string&, const Poco::Any&);
/// The setter method for a property.
typedef Poco::Any (C::*PropertyGetter)(const std::string&);
/// The getter method for a property.
AbstractSessionImpl(const std::string& connectionString,
std::size_t timeout = LOGIN_TIMEOUT_DEFAULT): SessionImpl(connectionString, timeout),
_storage(std::string("deque")),
_bulk(false),
_emptyStringIsNull(false),
_forceEmptyString(false)
/// Creates the AbstractSessionImpl.
///
/// Adds "storage" property and sets the default internal storage container
/// type to std::deque.
/// The storage is created by statements automatically whenever a query
/// returning results is executed but external storage is provided by the user.
/// Storage type can be reconfigured at runtime both globally (for the
/// duration of the session) and locally (for a single statement execution only).
/// See StatementImpl for details on how this property is used at runtime.
///
/// Adds "handle" property which, if set by the back end, returns native handle
/// for the back end DB.
///
/// Adds "bulk" feature and sets it to false.
/// Bulk feature determines whether the session is capable of bulk operations.
/// Connectors that are capable of it must set this feature prior to attempting
/// bulk operations.
///
/// Adds "emptyStringIsNull" feature and sets it to false. This feature should be
/// set to true in order to modify the behavior of the databases that distinguish
/// between zero-length character strings as nulls. Setting this feature to true
/// shall disregard any difference between empty character strings and nulls,
/// causing the framework to treat them the same (i.e. behave like Oracle).
///
/// Adds "forceEmptyString" feature and sets it to false. This feature should be set
/// to true in order to force the databases that do not distinguish empty strings from
/// nulls (e.g. Oracle) to always report empty string.
///
/// The "emptyStringIsNull" and "forceEmptyString" features are mutually exclusive.
/// While these features can not both be true at the same time, they can both be false,
/// resulting in default underlying database behavior.
///
{
addProperty("storage",
&AbstractSessionImpl<C>::setStorage,
&AbstractSessionImpl<C>::getStorage);
addProperty("handle",
&AbstractSessionImpl<C>::setHandle,
&AbstractSessionImpl<C>::getHandle);
addFeature("bulk",
&AbstractSessionImpl<C>::setBulk,
&AbstractSessionImpl<C>::getBulk);
addFeature("emptyStringIsNull",
&AbstractSessionImpl<C>::setEmptyStringIsNull,
&AbstractSessionImpl<C>::getEmptyStringIsNull);
addFeature("forceEmptyString",
&AbstractSessionImpl<C>::setForceEmptyString,
&AbstractSessionImpl<C>::getForceEmptyString);
}
~AbstractSessionImpl()
/// Destroys the AbstractSessionImpl.
{
}
void setFeature(const std::string& name, bool state)
/// Looks a feature up in the features map
/// and calls the feature's setter, if there is one.
{
typename FeatureMap::const_iterator it = _features.find(name);
if (it != _features.end())
{
if (it->second.setter)
(static_cast<C*>(this)->*it->second.setter)(name, state);
else
throw NotImplementedException("set", name);
}
else throw NotSupportedException(name);
}
bool getFeature(const std::string& name)
/// Looks a feature up in the features map
/// and calls the feature's getter, if there is one.
{
typename FeatureMap::const_iterator it = _features.find(name);
if (it != _features.end())
{
if (it->second.getter)
return (static_cast<C*>(this)->*it->second.getter)(name);
else
throw NotImplementedException("get", name);
}
else throw NotSupportedException(name);
}
void setProperty(const std::string& name, const Poco::Any& value)
/// Looks a property up in the properties map
/// and calls the property's setter, if there is one.
{
typename PropertyMap::const_iterator it = _properties.find(name);
if (it != _properties.end())
{
if (it->second.setter)
(static_cast<C*>(this)->*it->second.setter)(name, value);
else
throw NotImplementedException("set", name);
}
else throw NotSupportedException(name);
}
Poco::Any getProperty(const std::string& name)
/// Looks a property up in the properties map
/// and calls the property's getter, if there is one.
{
typename PropertyMap::const_iterator it = _properties.find(name);
if (it != _properties.end())
{
if (it->second.getter)
return (static_cast<C*>(this)->*it->second.getter)(name);
else
throw NotImplementedException("set", name);
}
else throw NotSupportedException(name);
}
void setStorage(const std::string& value)
/// Sets the storage type.
{
_storage = value;
}
void setStorage(const std::string& name, const Poco::Any& value)
/// Sets the storage type.
{
_storage = Poco::RefAnyCast<std::string>(value);
}
Poco::Any getStorage(const std::string& name="")
/// Returns the storage type
{
return _storage;
}
void setHandle(const std::string& name, const Poco::Any& handle)
/// Sets the native session handle.
{
_handle = handle;
}
Poco::Any getHandle(const std::string& name="")
/// Returns the native session handle.
{
return _handle;
}
void setBulk(const std::string& name, bool bulk)
/// Sets the execution type.
{
_bulk = bulk;
}
bool getBulk(const std::string& name="")
/// Returns the execution type
{
return _bulk;
}
void setEmptyStringIsNull(const std::string& name, bool emptyStringIsNull)
/// Sets the behavior regarding empty variable length strings.
/// Those are treated as NULL by Oracle and as empty string by
/// most other databases.
/// When this feature is true, empty strings are treated as NULL.
{
if (emptyStringIsNull && _forceEmptyString)
throw InvalidAccessException("Features mutually exclusive");
_emptyStringIsNull = emptyStringIsNull;
}
bool getEmptyStringIsNull(const std::string& name="")
/// Returns the setting for the behavior regarding empty variable
/// length strings. See setEmptyStringIsNull(const std::string&, bool)
/// and this class documentation for feature rationale and details.
{
return _emptyStringIsNull;
}
void setForceEmptyString(const std::string& name, bool forceEmptyString)
/// Sets the behavior regarding empty variable length strings.
/// Those are treated as NULL by Oracle and as empty string by
/// most other databases.
/// When this feature is true, both empty strings and NULL values
/// are reported as empty strings.
{
if (forceEmptyString && _emptyStringIsNull)
throw InvalidAccessException("Features mutually exclusive");
_forceEmptyString = forceEmptyString;
}
bool getForceEmptyString(const std::string& name="")
/// Returns the setting for the behavior regarding empty variable
/// length strings. See setForceEmptyString(const std::string&, bool)
/// and this class documentation for feature rationale and details.
{
return _forceEmptyString;
}
protected:
void addFeature(const std::string& name, FeatureSetter setter, FeatureGetter getter)
/// Adds a feature to the map of supported features.
///
/// The setter or getter can be null, in case setting or getting a feature
/// is not supported.
{
Feature feature;
feature.setter = setter;
feature.getter = getter;
_features[name] = feature;
}
void addProperty(const std::string& name, PropertySetter setter, PropertyGetter getter)
/// Adds a property to the map of supported properties.
///
/// The setter or getter can be null, in case setting or getting a property
/// is not supported.
{
Property property;
property.setter = setter;
property.getter = getter;
_properties[name] = property;
}
private:
struct Feature
{
FeatureSetter setter;
FeatureGetter getter;
};
struct Property
{
PropertySetter setter;
PropertyGetter getter;
};
typedef std::map<std::string, Feature> FeatureMap;
typedef std::map<std::string, Property> PropertyMap;
FeatureMap _features;
PropertyMap _properties;
std::string _storage;
bool _bulk;
bool _emptyStringIsNull;
bool _forceEmptyString;
Poco::Any _handle;
};
} } // namespace Poco::Data
template <class C>
class AbstractSessionImpl : public SessionImpl
/// A partial implementation of SessionImpl, providing
/// features and properties management.
///
/// To implement a certain feature or property, a subclass
/// must provide setter and getter methods and register
/// them with addFeature() or addProperty().
{
public:
typedef void (C::*FeatureSetter)(const std::string &, bool);
/// The setter method for a feature.
typedef bool (C::*FeatureGetter)(const std::string &);
/// The getter method for a feature.
typedef void (C::*PropertySetter)(const std::string &, const Poco::Any &);
/// The setter method for a property.
typedef Poco::Any (C::*PropertyGetter)(const std::string &);
/// The getter method for a property.
AbstractSessionImpl(const std::string & connectionString, std::size_t timeout = LOGIN_TIMEOUT_DEFAULT)
: SessionImpl(connectionString, timeout)
, _storage(std::string("deque"))
, _bulk(false)
, _emptyStringIsNull(false)
, _forceEmptyString(false)
/// Creates the AbstractSessionImpl.
///
/// Adds "storage" property and sets the default internal storage container
/// type to std::deque.
/// The storage is created by statements automatically whenever a query
/// returning results is executed but external storage is provided by the user.
/// Storage type can be reconfigured at runtime both globally (for the
/// duration of the session) and locally (for a single statement execution only).
/// See StatementImpl for details on how this property is used at runtime.
///
/// Adds "handle" property which, if set by the back end, returns native handle
/// for the back end DB.
///
/// Adds "bulk" feature and sets it to false.
/// Bulk feature determines whether the session is capable of bulk operations.
/// Connectors that are capable of it must set this feature prior to attempting
/// bulk operations.
///
/// Adds "emptyStringIsNull" feature and sets it to false. This feature should be
/// set to true in order to modify the behavior of the databases that distinguish
/// between zero-length character strings as nulls. Setting this feature to true
/// shall disregard any difference between empty character strings and nulls,
/// causing the framework to treat them the same (i.e. behave like Oracle).
///
/// Adds "forceEmptyString" feature and sets it to false. This feature should be set
/// to true in order to force the databases that do not distinguish empty strings from
/// nulls (e.g. Oracle) to always report empty string.
///
/// The "emptyStringIsNull" and "forceEmptyString" features are mutually exclusive.
/// While these features can not both be true at the same time, they can both be false,
/// resulting in default underlying database behavior.
///
{
addProperty("storage", &AbstractSessionImpl<C>::setStorage, &AbstractSessionImpl<C>::getStorage);
addProperty("handle", &AbstractSessionImpl<C>::setHandle, &AbstractSessionImpl<C>::getHandle);
addFeature("bulk", &AbstractSessionImpl<C>::setBulk, &AbstractSessionImpl<C>::getBulk);
addFeature("emptyStringIsNull", &AbstractSessionImpl<C>::setEmptyStringIsNull, &AbstractSessionImpl<C>::getEmptyStringIsNull);
addFeature("forceEmptyString", &AbstractSessionImpl<C>::setForceEmptyString, &AbstractSessionImpl<C>::getForceEmptyString);
}
~AbstractSessionImpl()
/// Destroys the AbstractSessionImpl.
{
}
void setFeature(const std::string & name, bool state)
/// Looks a feature up in the features map
/// and calls the feature's setter, if there is one.
{
typename FeatureMap::const_iterator it = _features.find(name);
if (it != _features.end())
{
if (it->second.setter)
(static_cast<C *>(this)->*it->second.setter)(name, state);
else
throw NotImplementedException("set", name);
}
else
throw NotSupportedException(name);
}
bool getFeature(const std::string & name)
/// Looks a feature up in the features map
/// and calls the feature's getter, if there is one.
{
typename FeatureMap::const_iterator it = _features.find(name);
if (it != _features.end())
{
if (it->second.getter)
return (static_cast<C *>(this)->*it->second.getter)(name);
else
throw NotImplementedException("get", name);
}
else
throw NotSupportedException(name);
}
void setProperty(const std::string & name, const Poco::Any & value)
/// Looks a property up in the properties map
/// and calls the property's setter, if there is one.
{
typename PropertyMap::const_iterator it = _properties.find(name);
if (it != _properties.end())
{
if (it->second.setter)
(static_cast<C *>(this)->*it->second.setter)(name, value);
else
throw NotImplementedException("set", name);
}
else
throw NotSupportedException(name);
}
Poco::Any getProperty(const std::string & name)
/// Looks a property up in the properties map
/// and calls the property's getter, if there is one.
{
typename PropertyMap::const_iterator it = _properties.find(name);
if (it != _properties.end())
{
if (it->second.getter)
return (static_cast<C *>(this)->*it->second.getter)(name);
else
throw NotImplementedException("set", name);
}
else
throw NotSupportedException(name);
}
void setStorage(const std::string & value)
/// Sets the storage type.
{
_storage = value;
}
void setStorage(const std::string & name, const Poco::Any & value)
/// Sets the storage type.
{
_storage = Poco::RefAnyCast<std::string>(value);
}
Poco::Any getStorage(const std::string & name = "")
/// Returns the storage type
{
return _storage;
}
void setHandle(const std::string & name, const Poco::Any & handle)
/// Sets the native session handle.
{
_handle = handle;
}
Poco::Any getHandle(const std::string & name = "")
/// Returns the native session handle.
{
return _handle;
}
void setBulk(const std::string & name, bool bulk)
/// Sets the execution type.
{
_bulk = bulk;
}
bool getBulk(const std::string & name = "")
/// Returns the execution type
{
return _bulk;
}
void setEmptyStringIsNull(const std::string & name, bool emptyStringIsNull)
/// Sets the behavior regarding empty variable length strings.
/// Those are treated as NULL by Oracle and as empty string by
/// most other databases.
/// When this feature is true, empty strings are treated as NULL.
{
if (emptyStringIsNull && _forceEmptyString)
throw InvalidAccessException("Features mutually exclusive");
_emptyStringIsNull = emptyStringIsNull;
}
bool getEmptyStringIsNull(const std::string & name = "")
/// Returns the setting for the behavior regarding empty variable
/// length strings. See setEmptyStringIsNull(const std::string&, bool)
/// and this class documentation for feature rationale and details.
{
return _emptyStringIsNull;
}
void setForceEmptyString(const std::string & name, bool forceEmptyString)
/// Sets the behavior regarding empty variable length strings.
/// Those are treated as NULL by Oracle and as empty string by
/// most other databases.
/// When this feature is true, both empty strings and NULL values
/// are reported as empty strings.
{
if (forceEmptyString && _emptyStringIsNull)
throw InvalidAccessException("Features mutually exclusive");
_forceEmptyString = forceEmptyString;
}
bool getForceEmptyString(const std::string & name = "")
/// Returns the setting for the behavior regarding empty variable
/// length strings. See setForceEmptyString(const std::string&, bool)
/// and this class documentation for feature rationale and details.
{
return _forceEmptyString;
}
protected:
void addFeature(const std::string & name, FeatureSetter setter, FeatureGetter getter)
/// Adds a feature to the map of supported features.
///
/// The setter or getter can be null, in case setting or getting a feature
/// is not supported.
{
Feature feature;
feature.setter = setter;
feature.getter = getter;
_features[name] = feature;
}
void addProperty(const std::string & name, PropertySetter setter, PropertyGetter getter)
/// Adds a property to the map of supported properties.
///
/// The setter or getter can be null, in case setting or getting a property
/// is not supported.
{
Property property;
property.setter = setter;
property.getter = getter;
_properties[name] = property;
}
private:
struct Feature
{
FeatureSetter setter;
FeatureGetter getter;
};
struct Property
{
PropertySetter setter;
PropertyGetter getter;
};
typedef std::map<std::string, Feature> FeatureMap;
typedef std::map<std::string, Property> PropertyMap;
FeatureMap _features;
PropertyMap _properties;
std::string _storage;
bool _bulk;
bool _emptyStringIsNull;
bool _forceEmptyString;
Poco::Any _handle;
};
}
} // namespace Poco::Data
#endif // Data_AbstractSessionImpl_INCLUDED

View File

@ -21,202 +21,206 @@
#include "Poco/Data/Data.h"
#include "Poco/Data/Session.h"
#include "Poco/DateTime.h"
#include "Poco/Timespan.h"
#include "Poco/Dynamic/Var.h"
#include "Poco/SharedPtr.h"
#include "Poco/Timespan.h"
namespace Poco {
namespace Data {
class Data_API ArchiveStrategy
/// The ArchiveStrategy is used by SQLChannel to archive log rows.
namespace Poco
{
public:
static const std::string DEFAULT_ARCHIVE_DESTINATION;
ArchiveStrategy(const std::string& connector,
const std::string& connect,
const std::string& source,
const std::string& destination = DEFAULT_ARCHIVE_DESTINATION);
/// Creates archive strategy.
virtual ~ArchiveStrategy();
/// Destroys archive strategy.
void open();
/// Opens the session.
virtual void archive() = 0;
/// Archives the rows.
const std::string& getSource() const;
/// Returns the name of the source table containing rows to be archived.
void setSource(const std::string& source);
/// Sets the name of the source table.
const std::string& getDestination() const;
/// Returns the name of the destination table for rows to be archived.
void setDestination(const std::string& destination);
/// Sets the name of the destination table.
virtual const std::string& getThreshold() const = 0;
/// Returns the archive threshold.
virtual void setThreshold(const std::string& threshold) = 0;
/// Sets the archive threshold.
protected:
typedef Poco::SharedPtr<Session> SessionPtr;
typedef Poco::SharedPtr<Statement> StatementPtr;
Session& session();
void setCopyStatement();
void setDeleteStatement();
void setCountStatement();
Statement& getCopyStatement();
Statement& getDeleteStatement();
Statement& getCountStatement();
private:
ArchiveStrategy();
ArchiveStrategy(const ArchiveStrategy&);
ArchiveStrategy& operator = (const ArchiveStrategy&);
std::string _connector;
std::string _connect;
SessionPtr _pSession;
StatementPtr _pCopyStatement;
StatementPtr _pDeleteStatement;
StatementPtr _pCountStatement;
std::string _source;
std::string _destination;
};
//
// inlines
//
inline const std::string& ArchiveStrategy::getSource() const
namespace Data
{
return _source;
class Data_API ArchiveStrategy
/// The ArchiveStrategy is used by SQLChannel to archive log rows.
{
public:
static const std::string DEFAULT_ARCHIVE_DESTINATION;
ArchiveStrategy(
const std::string & connector,
const std::string & connect,
const std::string & source,
const std::string & destination = DEFAULT_ARCHIVE_DESTINATION);
/// Creates archive strategy.
virtual ~ArchiveStrategy();
/// Destroys archive strategy.
void open();
/// Opens the session.
virtual void archive() = 0;
/// Archives the rows.
const std::string & getSource() const;
/// Returns the name of the source table containing rows to be archived.
void setSource(const std::string & source);
/// Sets the name of the source table.
const std::string & getDestination() const;
/// Returns the name of the destination table for rows to be archived.
void setDestination(const std::string & destination);
/// Sets the name of the destination table.
virtual const std::string & getThreshold() const = 0;
/// Returns the archive threshold.
virtual void setThreshold(const std::string & threshold) = 0;
/// Sets the archive threshold.
protected:
typedef Poco::SharedPtr<Session> SessionPtr;
typedef Poco::SharedPtr<Statement> StatementPtr;
Session & session();
void setCopyStatement();
void setDeleteStatement();
void setCountStatement();
Statement & getCopyStatement();
Statement & getDeleteStatement();
Statement & getCountStatement();
private:
ArchiveStrategy();
ArchiveStrategy(const ArchiveStrategy &);
ArchiveStrategy & operator=(const ArchiveStrategy &);
std::string _connector;
std::string _connect;
SessionPtr _pSession;
StatementPtr _pCopyStatement;
StatementPtr _pDeleteStatement;
StatementPtr _pCountStatement;
std::string _source;
std::string _destination;
};
//
// inlines
//
inline const std::string & ArchiveStrategy::getSource() const
{
return _source;
}
inline void ArchiveStrategy::setSource(const std::string & source)
{
_source = source;
}
inline void ArchiveStrategy::setDestination(const std::string & destination)
{
_destination = destination;
}
inline const std::string & ArchiveStrategy::getDestination() const
{
return _destination;
}
inline Session & ArchiveStrategy::session()
{
return *_pSession;
}
inline void ArchiveStrategy::setCopyStatement()
{
_pCopyStatement = new Statement(*_pSession);
}
inline void ArchiveStrategy::setDeleteStatement()
{
_pDeleteStatement = new Statement(*_pSession);
}
inline void ArchiveStrategy::setCountStatement()
{
_pCountStatement = new Statement(*_pSession);
}
inline Statement & ArchiveStrategy::getCopyStatement()
{
return *_pCopyStatement;
}
inline Statement & ArchiveStrategy::getDeleteStatement()
{
return *_pDeleteStatement;
}
inline Statement & ArchiveStrategy::getCountStatement()
{
return *_pCountStatement;
}
//
// ArchiveByAgeStrategy
//
class Data_API ArchiveByAgeStrategy : public ArchiveStrategy
/// Archives rows scheduled for archiving.
{
public:
ArchiveByAgeStrategy(
const std::string & connector,
const std::string & connect,
const std::string & sourceTable,
const std::string & destinationTable = DEFAULT_ARCHIVE_DESTINATION);
~ArchiveByAgeStrategy();
void archive();
const std::string & getThreshold() const;
/// Returns the archive threshold.
void setThreshold(const std::string & threshold);
/// Sets the archive threshold.
private:
ArchiveByAgeStrategy();
ArchiveByAgeStrategy(const ArchiveByAgeStrategy &);
ArchiveByAgeStrategy & operator=(const ArchiveByAgeStrategy &);
void initStatements();
Timespan _maxAge;
std::string _ageString;
DateTime _archiveDateTime;
Poco::Dynamic::Var _archiveCount;
};
//
// inlines
//
inline const std::string & ArchiveByAgeStrategy::getThreshold() const
{
return _ageString;
}
}
inline void ArchiveStrategy::setSource(const std::string& source)
{
_source = source;
}
inline void ArchiveStrategy::setDestination(const std::string& destination)
{
_destination = destination;
}
inline const std::string& ArchiveStrategy::getDestination() const
{
return _destination;
}
inline Session& ArchiveStrategy::session()
{
return *_pSession;
}
inline void ArchiveStrategy::setCopyStatement()
{
_pCopyStatement = new Statement(*_pSession);
}
inline void ArchiveStrategy::setDeleteStatement()
{
_pDeleteStatement = new Statement(*_pSession);
}
inline void ArchiveStrategy::setCountStatement()
{
_pCountStatement = new Statement(*_pSession);
}
inline Statement& ArchiveStrategy::getCopyStatement()
{
return *_pCopyStatement;
}
inline Statement& ArchiveStrategy::getDeleteStatement()
{
return *_pDeleteStatement;
}
inline Statement& ArchiveStrategy::getCountStatement()
{
return *_pCountStatement;
}
//
// ArchiveByAgeStrategy
//
class Data_API ArchiveByAgeStrategy: public ArchiveStrategy
/// Archives rows scheduled for archiving.
{
public:
ArchiveByAgeStrategy(const std::string& connector,
const std::string& connect,
const std::string& sourceTable,
const std::string& destinationTable = DEFAULT_ARCHIVE_DESTINATION);
~ArchiveByAgeStrategy();
void archive();
const std::string& getThreshold() const;
/// Returns the archive threshold.
void setThreshold(const std::string& threshold);
/// Sets the archive threshold.
private:
ArchiveByAgeStrategy();
ArchiveByAgeStrategy(const ArchiveByAgeStrategy&);
ArchiveByAgeStrategy& operator = (const ArchiveByAgeStrategy&);
void initStatements();
Timespan _maxAge;
std::string _ageString;
DateTime _archiveDateTime;
Poco::Dynamic::Var _archiveCount;
};
//
// inlines
//
inline const std::string& ArchiveByAgeStrategy::getThreshold() const
{
return _ageString;
}
} } // namespace Poco::Data
} // namespace Poco::Data
#endif // Data_ArchiveStrategy_INCLUDED

View File

@ -14,7 +14,6 @@
//
#ifndef Data_AutoTransaction_INCLUDED
#define Data_AutoTransaction_INCLUDED
@ -22,14 +21,17 @@
#include "Poco/Data/Transaction.h"
namespace Poco {
namespace Data {
namespace Poco
{
namespace Data
{
typedef Transaction AutoTransaction;
typedef Transaction AutoTransaction;
} } // namespace Poco::Data
}
} // namespace Poco::Data
#endif // Data_AutoTransaction_INCLUDED

File diff suppressed because it is too large Load Diff

View File

@ -18,79 +18,83 @@
#define Data_Bulk_INCLUDED
#include "Poco/Void.h"
#include "Poco/Data/Limit.h"
#include "Poco/Void.h"
namespace Poco {
namespace Data {
class Data_API Bulk
namespace Poco
{
public:
Bulk(const Limit& limit);
/// Creates the Bulk.
Bulk(Poco::UInt32 value);
/// Creates the Bulk.
~Bulk();
/// Destroys the bulk.
const Limit& limit() const;
/// Returns the limit associated with this bulk object.
Poco::UInt32 size() const;
/// Returns the value of the limit associated with
/// this bulk object.
private:
Bulk();
Limit _limit;
};
///
/// inlines
///
inline const Limit& Bulk::limit() const
{
return _limit;
}
inline Poco::UInt32 Bulk::size() const
{
return _limit.value();
}
namespace Keywords {
inline Bulk bulk(const Limit& limit = Limit(Limit::LIMIT_UNLIMITED, false, false))
/// Convenience function for creation of bulk.
namespace Data
{
return Bulk(limit);
class Data_API Bulk
{
public:
Bulk(const Limit & limit);
/// Creates the Bulk.
Bulk(Poco::UInt32 value);
/// Creates the Bulk.
~Bulk();
/// Destroys the bulk.
const Limit & limit() const;
/// Returns the limit associated with this bulk object.
Poco::UInt32 size() const;
/// Returns the value of the limit associated with
/// this bulk object.
private:
Bulk();
Limit _limit;
};
///
/// inlines
///
inline const Limit & Bulk::limit() const
{
return _limit;
}
inline Poco::UInt32 Bulk::size() const
{
return _limit.value();
}
namespace Keywords
{
inline Bulk bulk(const Limit & limit = Limit(Limit::LIMIT_UNLIMITED, false, false))
/// Convenience function for creation of bulk.
{
return Bulk(limit);
}
inline void bulk(Void)
/// Dummy bulk function. Used for bulk binding creation
/// (see BulkBinding) and bulk extraction signalling to Statement.
{
}
} // namespace Keywords
typedef void (*BulkFnType)(Void);
}
inline void bulk(Void)
/// Dummy bulk function. Used for bulk binding creation
/// (see BulkBinding) and bulk extraction signalling to Statement.
{
}
} // namespace Keywords
typedef void (*BulkFnType)(Void);
} } // namespace Poco::Data
} // namespace Poco::Data
#endif // Data_Bulk_INCLUDED

View File

@ -18,131 +18,124 @@
#define Data_BulkBinding_INCLUDED
#include "Poco/Data/Data.h"
#include "Poco/Data/AbstractBinding.h"
#include "Poco/Data/DataException.h"
#include "Poco/Data/TypeHandler.h"
#include "Poco/Data/Bulk.h"
#include <vector>
#include <cstddef>
#include <deque>
#include <list>
#include <cstddef>
#include <vector>
#include "Poco/Data/AbstractBinding.h"
#include "Poco/Data/Bulk.h"
#include "Poco/Data/Data.h"
#include "Poco/Data/DataException.h"
#include "Poco/Data/TypeHandler.h"
namespace Poco {
namespace Data {
template <class T>
class BulkBinding: public AbstractBinding
/// A BulkBinding maps a value to a column.
/// Bulk binding support is provided only for std::vector.
namespace Poco
{
public:
BulkBinding(const T& val, Poco::UInt32 bulkSize, const std::string& name = "", Direction direction = PD_IN):
AbstractBinding(name, direction, bulkSize),
_val(val),
_bound(false)
/// Creates the BulkBinding.
{
if (0 == _val.size())
throw BindingException("Zero size containers not allowed.");
}
~BulkBinding()
/// Destroys the BulkBinding.
{
}
std::size_t numOfColumnsHandled() const
{
return 1;
}
std::size_t numOfRowsHandled() const
{
return _val.size();
}
bool canBind() const
{
return !_bound;
}
void bind(std::size_t pos)
{
poco_assert_dbg(!getBinder().isNull());
TypeHandler<T>::bind(pos, _val, getBinder(), getDirection());
_bound = true;
}
void reset ()
{
_bound = false;
getBinder()->reset();
}
private:
const T& _val;
bool _bound;
};
namespace Keywords {
template <typename T>
AbstractBinding::Ptr use(const std::vector<T>& t, BulkFnType, const std::string& name = "")
/// Convenience function for a more compact BulkBinding creation for std::vector.
namespace Data
{
return new BulkBinding<std::vector<T> >(t, static_cast<Poco::UInt32>(t.size()), name);
template <class T>
class BulkBinding : public AbstractBinding
/// A BulkBinding maps a value to a column.
/// Bulk binding support is provided only for std::vector.
{
public:
BulkBinding(const T & val, Poco::UInt32 bulkSize, const std::string & name = "", Direction direction = PD_IN)
: AbstractBinding(name, direction, bulkSize), _val(val), _bound(false)
/// Creates the BulkBinding.
{
if (0 == _val.size())
throw BindingException("Zero size containers not allowed.");
}
~BulkBinding()
/// Destroys the BulkBinding.
{
}
std::size_t numOfColumnsHandled() const { return 1; }
std::size_t numOfRowsHandled() const { return _val.size(); }
bool canBind() const { return !_bound; }
void bind(std::size_t pos)
{
poco_assert_dbg(!getBinder().isNull());
TypeHandler<T>::bind(pos, _val, getBinder(), getDirection());
_bound = true;
}
void reset()
{
_bound = false;
getBinder()->reset();
}
private:
const T & _val;
bool _bound;
};
namespace Keywords
{
template <typename T>
AbstractBinding::Ptr use(const std::vector<T> & t, BulkFnType, const std::string & name = "")
/// Convenience function for a more compact BulkBinding creation for std::vector.
{
return new BulkBinding<std::vector<T>>(t, static_cast<Poco::UInt32>(t.size()), name);
}
template <typename T>
AbstractBinding::Ptr in(const std::vector<T> & t, BulkFnType, const std::string & name = "")
/// Convenience function for a more compact BulkBinding creation for std::vector.
{
return new BulkBinding<std::vector<T>>(t, static_cast<Poco::UInt32>(t.size()), name);
}
template <typename T>
AbstractBinding::Ptr use(const std::deque<T> & t, BulkFnType, const std::string & name = "")
/// Convenience function for a more compact BulkBinding creation for std::deque.
{
return new BulkBinding<std::deque<T>>(t, static_cast<Poco::UInt32>(t.size()), name);
}
template <typename T>
AbstractBinding::Ptr in(const std::deque<T> & t, BulkFnType, const std::string & name = "")
/// Convenience function for a more compact BulkBinding creation for std::deque.
{
return new BulkBinding<std::deque<T>>(t, static_cast<Poco::UInt32>(t.size()), name);
}
template <typename T>
AbstractBinding::Ptr use(const std::list<T> & t, BulkFnType, const std::string & name = "")
/// Convenience function for a more compact BulkBinding creation for std::list.
{
return new BulkBinding<std::list<T>>(t, static_cast<Poco::UInt32>(t.size()), name);
}
template <typename T>
AbstractBinding::Ptr in(const std::list<T> & t, BulkFnType, const std::string & name = "")
/// Convenience function for a more compact BulkBinding creation for std::list.
{
return new BulkBinding<std::list<T>>(t, static_cast<Poco::UInt32>(t.size()), name);
}
} // namespace Keywords
}
template <typename T>
AbstractBinding::Ptr in(const std::vector<T>& t, BulkFnType, const std::string& name = "")
/// Convenience function for a more compact BulkBinding creation for std::vector.
{
return new BulkBinding<std::vector<T> >(t, static_cast<Poco::UInt32>(t.size()), name);
}
template <typename T>
AbstractBinding::Ptr use(const std::deque<T>& t, BulkFnType, const std::string& name = "")
/// Convenience function for a more compact BulkBinding creation for std::deque.
{
return new BulkBinding<std::deque<T> >(t, static_cast<Poco::UInt32>(t.size()), name);
}
template <typename T>
AbstractBinding::Ptr in(const std::deque<T>& t, BulkFnType, const std::string& name = "")
/// Convenience function for a more compact BulkBinding creation for std::deque.
{
return new BulkBinding<std::deque<T> >(t, static_cast<Poco::UInt32>(t.size()), name);
}
template <typename T>
AbstractBinding::Ptr use(const std::list<T>& t, BulkFnType, const std::string& name = "")
/// Convenience function for a more compact BulkBinding creation for std::list.
{
return new BulkBinding<std::list<T> >(t, static_cast<Poco::UInt32>(t.size()), name);
}
template <typename T>
AbstractBinding::Ptr in(const std::list<T>& t, BulkFnType, const std::string& name = "")
/// Convenience function for a more compact BulkBinding creation for std::list.
{
return new BulkBinding<std::list<T> >(t, static_cast<Poco::UInt32>(t.size()), name);
}
} // namespace Keywords
} } // namespace Poco::Data
} // namespace Poco::Data
#endif // Data_BulkBinding_INCLUDED

View File

@ -18,259 +18,236 @@
#define Data_BulkExtraction_INCLUDED
#include "Poco/Data/Data.h"
#include <vector>
#include "Poco/Data/AbstractExtraction.h"
#include "Poco/Data/Bulk.h"
#include "Poco/Data/Column.h"
#include "Poco/Data/Data.h"
#include "Poco/Data/Position.h"
#include "Poco/Data/Preparation.h"
#include <vector>
namespace Poco {
namespace Data {
template <class C>
class BulkExtraction: public AbstractExtraction
/// Specialization for bulk extraction of values from a query result set.
/// Bulk extraction support is provided only for following STL containers:
/// - std::vector
/// - std::deque
/// - std::list
namespace Poco
{
public:
typedef C ValType;
typedef typename C::value_type CValType;
typedef SharedPtr<ValType> ValPtr;
typedef BulkExtraction<ValType> Type;
typedef SharedPtr<Type> Ptr;
BulkExtraction(C& result, Poco::UInt32 limit, const Position& pos = Position(0)):
AbstractExtraction(limit, pos.value(), true),
_rResult(result),
_default()
{
if (static_cast<Poco::UInt32>(result.size()) != limit)
result.resize(limit);
}
BulkExtraction(C& result, const CValType& def, Poco::UInt32 limit, const Position& pos = Position(0)):
AbstractExtraction(limit, pos.value(), true),
_rResult(result),
_default(def)
{
if (static_cast<Poco::UInt32>(result.size()) != limit)
result.resize(limit);
}
virtual ~BulkExtraction()
{
}
std::size_t numOfColumnsHandled() const
{
return TypeHandler<C>::size();
}
std::size_t numOfRowsHandled() const
{
return _rResult.size();
}
std::size_t numOfRowsAllowed() const
{
return getLimit();
}
bool isNull(std::size_t row) const
{
try
{
return _nulls.at(row);
}
catch (std::out_of_range& ex)
{
throw RangeException(ex.what());
}
}
std::size_t extract(std::size_t col)
{
AbstractExtractor::Ptr pExt = getExtractor();
TypeHandler<C>::extract(col, _rResult, _default, pExt);
typename C::iterator it = _rResult.begin();
typename C::iterator end = _rResult.end();
for (int row = 0; it !=end; ++it, ++row)
{
_nulls.push_back(isValueNull(*it, pExt->isNull(col, row)));
}
return _rResult.size();
}
virtual void reset()
{
}
AbstractPreparation::Ptr createPreparation(AbstractPreparator::Ptr& pPrep, std::size_t col)
{
Poco::UInt32 limit = getLimit();
if (limit != _rResult.size()) _rResult.resize(limit);
pPrep->setLength(limit);
pPrep->setBulk(true);
return new Preparation<C>(pPrep, col, _rResult);
}
protected:
const C& result() const
{
return _rResult;
}
private:
C& _rResult;
CValType _default;
std::deque<bool> _nulls;
};
template <class C>
class InternalBulkExtraction: public BulkExtraction<C>
/// Container Data Type specialization extension for extraction of values from a query result set.
///
/// This class is intended for PocoData internal use - it is used by StatementImpl
/// to automatically create internal BulkExtraction in cases when statement returns data and no external storage
/// was supplied. It is later used by RecordSet to retrieve the fetched data after statement execution.
/// It takes ownership of the Column pointer supplied as constructor argument. Column object, in turn
/// owns the data container pointer.
///
/// InternalBulkExtraction objects can not be copied or assigned.
namespace Data
{
public:
typedef C ValType;
typedef typename C::value_type CValType;
typedef SharedPtr<ValType> ValPtr;
typedef InternalBulkExtraction<ValType> Type;
typedef SharedPtr<Type> Ptr;
InternalBulkExtraction(C& result,
Column<C>* pColumn,
Poco::UInt32 limit,
const Position& pos = Position(0)):
BulkExtraction<C>(result, CValType(), limit, pos),
_pColumn(pColumn)
/// Creates InternalBulkExtraction.
{
}
~InternalBulkExtraction()
/// Destroys InternalBulkExtraction.
{
delete _pColumn;
}
void reset()
{
_pColumn->reset();
}
const CValType& value(int index) const
{
try
{
return BulkExtraction<C>::result().at(index);
}
catch (std::out_of_range& ex)
{
throw RangeException(ex.what());
}
}
bool isNull(std::size_t row) const
{
return BulkExtraction<C>::isNull(row);
}
const Column<C>& column() const
{
return *_pColumn;
}
private:
InternalBulkExtraction();
InternalBulkExtraction(const InternalBulkExtraction&);
InternalBulkExtraction& operator = (const InternalBulkExtraction&);
Column<C>* _pColumn;
};
namespace Keywords {
template <class C>
class BulkExtraction : public AbstractExtraction
/// Specialization for bulk extraction of values from a query result set.
/// Bulk extraction support is provided only for following STL containers:
/// - std::vector
/// - std::deque
/// - std::list
{
public:
typedef C ValType;
typedef typename C::value_type CValType;
typedef SharedPtr<ValType> ValPtr;
typedef BulkExtraction<ValType> Type;
typedef SharedPtr<Type> Ptr;
BulkExtraction(C & result, Poco::UInt32 limit, const Position & pos = Position(0))
: AbstractExtraction(limit, pos.value(), true), _rResult(result), _default()
{
if (static_cast<Poco::UInt32>(result.size()) != limit)
result.resize(limit);
}
BulkExtraction(C & result, const CValType & def, Poco::UInt32 limit, const Position & pos = Position(0))
: AbstractExtraction(limit, pos.value(), true), _rResult(result), _default(def)
{
if (static_cast<Poco::UInt32>(result.size()) != limit)
result.resize(limit);
}
virtual ~BulkExtraction() { }
std::size_t numOfColumnsHandled() const { return TypeHandler<C>::size(); }
std::size_t numOfRowsHandled() const { return _rResult.size(); }
std::size_t numOfRowsAllowed() const { return getLimit(); }
bool isNull(std::size_t row) const
{
try
{
return _nulls.at(row);
}
catch (std::out_of_range & ex)
{
throw RangeException(ex.what());
}
}
std::size_t extract(std::size_t col)
{
AbstractExtractor::Ptr pExt = getExtractor();
TypeHandler<C>::extract(col, _rResult, _default, pExt);
typename C::iterator it = _rResult.begin();
typename C::iterator end = _rResult.end();
for (int row = 0; it != end; ++it, ++row)
{
_nulls.push_back(isValueNull(*it, pExt->isNull(col, row)));
}
return _rResult.size();
}
virtual void reset() { }
AbstractPreparation::Ptr createPreparation(AbstractPreparator::Ptr & pPrep, std::size_t col)
{
Poco::UInt32 limit = getLimit();
if (limit != _rResult.size())
_rResult.resize(limit);
pPrep->setLength(limit);
pPrep->setBulk(true);
return new Preparation<C>(pPrep, col, _rResult);
}
protected:
const C & result() const { return _rResult; }
private:
C & _rResult;
CValType _default;
std::deque<bool> _nulls;
};
template <class C>
class InternalBulkExtraction : public BulkExtraction<C>
/// Container Data Type specialization extension for extraction of values from a query result set.
///
/// This class is intended for PocoData internal use - it is used by StatementImpl
/// to automatically create internal BulkExtraction in cases when statement returns data and no external storage
/// was supplied. It is later used by RecordSet to retrieve the fetched data after statement execution.
/// It takes ownership of the Column pointer supplied as constructor argument. Column object, in turn
/// owns the data container pointer.
///
/// InternalBulkExtraction objects can not be copied or assigned.
{
public:
typedef C ValType;
typedef typename C::value_type CValType;
typedef SharedPtr<ValType> ValPtr;
typedef InternalBulkExtraction<ValType> Type;
typedef SharedPtr<Type> Ptr;
InternalBulkExtraction(C & result, Column<C> * pColumn, Poco::UInt32 limit, const Position & pos = Position(0))
: BulkExtraction<C>(result, CValType(), limit, pos), _pColumn(pColumn)
/// Creates InternalBulkExtraction.
{
}
~InternalBulkExtraction()
/// Destroys InternalBulkExtraction.
{
delete _pColumn;
}
void reset() { _pColumn->reset(); }
const CValType & value(int index) const
{
try
{
return BulkExtraction<C>::result().at(index);
}
catch (std::out_of_range & ex)
{
throw RangeException(ex.what());
}
}
bool isNull(std::size_t row) const { return BulkExtraction<C>::isNull(row); }
const Column<C> & column() const { return *_pColumn; }
private:
InternalBulkExtraction();
InternalBulkExtraction(const InternalBulkExtraction &);
InternalBulkExtraction & operator=(const InternalBulkExtraction &);
Column<C> * _pColumn;
};
namespace Keywords
{
template <typename T>
AbstractExtraction::Ptr into(std::vector<T> & t, const Bulk & bulk, const Position & pos = Position(0))
/// Convenience function to allow for a more compact creation of an extraction object
/// with std::vector bulk extraction support.
{
return new BulkExtraction<std::vector<T>>(t, bulk.size(), pos);
}
template <typename T>
AbstractExtraction::Ptr into(std::vector<T> & t, BulkFnType, const Position & pos = Position(0))
/// Convenience function to allow for a more compact creation of an extraction object
/// with std::vector bulk extraction support.
{
Poco::UInt32 size = static_cast<Poco::UInt32>(t.size());
if (0 == size)
throw InvalidArgumentException("Zero length not allowed.");
return new BulkExtraction<std::vector<T>>(t, size, pos);
}
template <typename T>
AbstractExtraction::Ptr into(std::deque<T> & t, const Bulk & bulk, const Position & pos = Position(0))
/// Convenience function to allow for a more compact creation of an extraction object
/// with std::deque bulk extraction support.
{
return new BulkExtraction<std::deque<T>>(t, bulk.size(), pos);
}
template <typename T>
AbstractExtraction::Ptr into(std::deque<T> & t, BulkFnType, const Position & pos = Position(0))
/// Convenience function to allow for a more compact creation of an extraction object
/// with std::deque bulk extraction support.
{
Poco::UInt32 size = static_cast<Poco::UInt32>(t.size());
if (0 == size)
throw InvalidArgumentException("Zero length not allowed.");
return new BulkExtraction<std::deque<T>>(t, size, pos);
}
template <typename T>
AbstractExtraction::Ptr into(std::list<T> & t, const Bulk & bulk, const Position & pos = Position(0))
/// Convenience function to allow for a more compact creation of an extraction object
/// with std::list bulk extraction support.
{
return new BulkExtraction<std::list<T>>(t, bulk.size(), pos);
}
template <typename T>
AbstractExtraction::Ptr into(std::list<T> & t, BulkFnType, const Position & pos = Position(0))
/// Convenience function to allow for a more compact creation of an extraction object
/// with std::list bulk extraction support.
{
Poco::UInt32 size = static_cast<Poco::UInt32>(t.size());
if (0 == size)
throw InvalidArgumentException("Zero length not allowed.");
return new BulkExtraction<std::list<T>>(t, size, pos);
}
} // namespace Keywords
template <typename T>
AbstractExtraction::Ptr into(std::vector<T>& t, const Bulk& bulk, const Position& pos = Position(0))
/// Convenience function to allow for a more compact creation of an extraction object
/// with std::vector bulk extraction support.
{
return new BulkExtraction<std::vector<T> >(t, bulk.size(), pos);
}
template <typename T>
AbstractExtraction::Ptr into(std::vector<T>& t, BulkFnType, const Position& pos = Position(0))
/// Convenience function to allow for a more compact creation of an extraction object
/// with std::vector bulk extraction support.
{
Poco::UInt32 size = static_cast<Poco::UInt32>(t.size());
if (0 == size) throw InvalidArgumentException("Zero length not allowed.");
return new BulkExtraction<std::vector<T> >(t, size, pos);
}
template <typename T>
AbstractExtraction::Ptr into(std::deque<T>& t, const Bulk& bulk, const Position& pos = Position(0))
/// Convenience function to allow for a more compact creation of an extraction object
/// with std::deque bulk extraction support.
{
return new BulkExtraction<std::deque<T> >(t, bulk.size(), pos);
}
template <typename T>
AbstractExtraction::Ptr into(std::deque<T>& t, BulkFnType, const Position& pos = Position(0))
/// Convenience function to allow for a more compact creation of an extraction object
/// with std::deque bulk extraction support.
{
Poco::UInt32 size = static_cast<Poco::UInt32>(t.size());
if (0 == size) throw InvalidArgumentException("Zero length not allowed.");
return new BulkExtraction<std::deque<T> >(t, size, pos);
}
template <typename T>
AbstractExtraction::Ptr into(std::list<T>& t, const Bulk& bulk, const Position& pos = Position(0))
/// Convenience function to allow for a more compact creation of an extraction object
/// with std::list bulk extraction support.
{
return new BulkExtraction<std::list<T> >(t, bulk.size(), pos);
}
template <typename T>
AbstractExtraction::Ptr into(std::list<T>& t, BulkFnType, const Position& pos = Position(0))
/// Convenience function to allow for a more compact creation of an extraction object
/// with std::list bulk extraction support.
{
Poco::UInt32 size = static_cast<Poco::UInt32>(t.size());
if (0 == size) throw InvalidArgumentException("Zero length not allowed.");
return new BulkExtraction<std::list<T> >(t, size, pos);
}
} // namespace Keywords
} } // namespace Poco::Data
} // namespace Poco::Data
#endif // Data_BulkExtraction_INCLUDED

View File

@ -18,473 +18,465 @@
#define Data_Column_INCLUDED
#include <deque>
#include <list>
#include <vector>
#include "Poco/Data/Data.h"
#include "Poco/Data/MetaColumn.h"
#include "Poco/SharedPtr.h"
#include "Poco/RefCountedObject.h"
#include <vector>
#include <list>
#include <deque>
#include "Poco/SharedPtr.h"
namespace Poco {
namespace Data {
template <class C>
class Column
/// Column class is column data container.
/// Data (a pointer to underlying STL container) is assigned to the class
/// at construction time. Construction with null pointer is not allowed.
/// This class owns the data assigned to it and deletes the storage on destruction.
namespace Poco
{
public:
typedef C Container;
typedef Poco::SharedPtr<C> ContainerPtr;
typedef typename C::const_iterator Iterator;
typedef typename C::const_reverse_iterator RIterator;
typedef typename C::size_type Size;
typedef typename C::value_type Type;
Column(const MetaColumn& metaColumn, Container* pData):
_metaColumn(metaColumn),
_pData(pData)
/// Creates the Column.
{
if (!_pData)
throw NullPointerException("Container pointer must point to valid storage.");
}
Column(const Column& col):
_metaColumn(col._metaColumn),
_pData(col._pData)
/// Creates the Column.
{
}
~Column()
/// Destroys the Column.
{
}
Column& operator = (const Column& col)
/// Assignment operator.
{
Column tmp(col);
swap(tmp);
return *this;
}
void swap(Column& other)
/// Swaps the column with another one.
{
using std::swap;
swap(_metaColumn, other._metaColumn);
swap(_pData, other._pData);
}
Container& data()
/// Returns reference to contained data.
{
return *_pData;
}
const Type& value(std::size_t row) const
/// Returns the field value in specified row.
{
try
{
return _pData->at(row);
}
catch (std::out_of_range& ex)
{
throw RangeException(ex.what());
}
}
const Type& operator [] (std::size_t row) const
/// Returns the field value in specified row.
{
return value(row);
}
Size rowCount() const
/// Returns number of rows.
{
return _pData->size();
}
void reset()
/// Clears and shrinks the storage.
{
Container().swap(*_pData);
}
const std::string& name() const
/// Returns column name.
{
return _metaColumn.name();
}
std::size_t length() const
/// Returns column maximum length.
{
return _metaColumn.length();
}
std::size_t precision() const
/// Returns column precision.
/// Valid for floating point fields only (zero for other data types).
{
return _metaColumn.precision();
}
std::size_t position() const
/// Returns column position.
{
return _metaColumn.position();
}
MetaColumn::ColumnDataType type() const
/// Returns column type.
{
return _metaColumn.type();
}
Iterator begin() const
/// Returns iterator pointing to the beginning of data storage vector.
{
return _pData->begin();
}
Iterator end() const
/// Returns iterator pointing to the end of data storage vector.
{
return _pData->end();
}
private:
Column();
MetaColumn _metaColumn;
ContainerPtr _pData;
};
template <>
class Column<std::vector<bool> >
/// The std::vector<bool> specialization for the Column class.
///
/// This specialization is necessary due to the nature of std::vector<bool>.
/// For details, see the standard library implementation of vector<bool>
/// or
/// S. Meyers: "Effective STL" (Copyright Addison-Wesley 2001),
/// Item 18: "Avoid using vector<bool>."
///
/// The workaround employed here is using deque<bool> as an
/// internal "companion" container kept in sync with the vector<bool>
/// column data.
namespace Data
{
public:
typedef std::vector<bool> Container;
typedef Poco::SharedPtr<Container> ContainerPtr;
typedef Container::const_iterator Iterator;
typedef Container::const_reverse_iterator RIterator;
typedef Container::size_type Size;
Column(const MetaColumn& metaColumn, Container* pData):
_metaColumn(metaColumn),
_pData(pData)
/// Creates the Column.
{
poco_check_ptr (_pData);
_deque.assign(_pData->begin(), _pData->end());
}
Column(const Column& col):
_metaColumn(col._metaColumn),
_pData(col._pData)
/// Creates the Column.
{
_deque.assign(_pData->begin(), _pData->end());
}
~Column()
/// Destroys the Column.
{
}
Column& operator = (const Column& col)
/// Assignment operator.
{
Column tmp(col);
swap(tmp);
return *this;
}
void swap(Column& other)
/// Swaps the column with another one.
{
using std::swap;
swap(_metaColumn, other._metaColumn);
swap(_pData, other._pData);
swap(_deque, other._deque);
}
Container& data()
/// Returns reference to contained data.
{
return *_pData;
}
const bool& value(std::size_t row) const
/// Returns the field value in specified row.
{
if (_deque.size() < _pData->size())
_deque.resize(_pData->size());
try
{
return _deque.at(row) = _pData->at(row);
}
catch (std::out_of_range& ex)
{
throw RangeException(ex.what());
}
}
const bool& operator [] (std::size_t row) const
/// Returns the field value in specified row.
{
return value(row);
}
Size rowCount() const
/// Returns number of rows.
{
return _pData->size();
}
void reset()
/// Clears and shrinks the storage.
{
Container().swap(*_pData);
_deque.clear();
}
const std::string& name() const
/// Returns column name.
{
return _metaColumn.name();
}
std::size_t length() const
/// Returns column maximum length.
{
return _metaColumn.length();
}
std::size_t precision() const
/// Returns column precision.
/// Valid for floating point fields only (zero for other data types).
{
return _metaColumn.precision();
}
std::size_t position() const
/// Returns column position.
{
return _metaColumn.position();
}
MetaColumn::ColumnDataType type() const
/// Returns column type.
{
return _metaColumn.type();
}
Iterator begin() const
/// Returns iterator pointing to the beginning of data storage vector.
{
return _pData->begin();
}
Iterator end() const
/// Returns iterator pointing to the end of data storage vector.
{
return _pData->end();
}
private:
Column();
MetaColumn _metaColumn;
ContainerPtr _pData;
mutable std::deque<bool> _deque;
};
template <class T>
class Column<std::list<T> >
/// Column specialization for std::list
{
public:
typedef std::list<T> Container;
typedef Poco::SharedPtr<Container> ContainerPtr;
typedef typename Container::const_iterator Iterator;
typedef typename Container::const_reverse_iterator RIterator;
typedef typename Container::size_type Size;
template <class C>
class Column
/// Column class is column data container.
/// Data (a pointer to underlying STL container) is assigned to the class
/// at construction time. Construction with null pointer is not allowed.
/// This class owns the data assigned to it and deletes the storage on destruction.
{
public:
typedef C Container;
typedef Poco::SharedPtr<C> ContainerPtr;
typedef typename C::const_iterator Iterator;
typedef typename C::const_reverse_iterator RIterator;
typedef typename C::size_type Size;
typedef typename C::value_type Type;
Column(const MetaColumn& metaColumn, std::list<T>* pData):
_metaColumn(metaColumn),
_pData(pData)
/// Creates the Column.
{
poco_check_ptr (_pData);
}
Column(const MetaColumn & metaColumn, Container * pData) : _metaColumn(metaColumn), _pData(pData)
/// Creates the Column.
{
if (!_pData)
throw NullPointerException("Container pointer must point to valid storage.");
}
Column(const Column& col):
_metaColumn(col._metaColumn),
_pData(col._pData)
/// Creates the Column.
{
}
Column(const Column & col) : _metaColumn(col._metaColumn), _pData(col._pData)
/// Creates the Column.
{
}
~Column()
/// Destroys the Column.
{
}
~Column()
/// Destroys the Column.
{
}
Column& operator = (const Column& col)
/// Assignment operator.
{
Column tmp(col);
swap(tmp);
return *this;
}
Column & operator=(const Column & col)
/// Assignment operator.
{
Column tmp(col);
swap(tmp);
return *this;
}
void swap(Column& other)
/// Swaps the column with another one.
{
using std::swap;
swap(_metaColumn, other._metaColumn);
swap(_pData, other._pData);
}
void swap(Column & other)
/// Swaps the column with another one.
{
using std::swap;
swap(_metaColumn, other._metaColumn);
swap(_pData, other._pData);
}
Container& data()
/// Returns reference to contained data.
{
return *_pData;
}
Container & data()
/// Returns reference to contained data.
{
return *_pData;
}
const T& value(std::size_t row) const
/// Returns the field value in specified row.
/// This is the std::list specialization and std::list
/// is not the optimal solution for cases where random
/// access is needed.
/// However, to allow for compatibility with other
/// containers, this functionality is provided here.
/// To alleviate the problem, an effort is made
/// to start iteration from beginning or end,
/// depending on the position requested.
{
if (row <= (std::size_t) (_pData->size() / 2))
{
Iterator it = _pData->begin();
Iterator end = _pData->end();
for (int i = 0; it != end; ++it, ++i)
if (i == row) return *it;
}
else
{
row = _pData->size() - row;
RIterator it = _pData->rbegin();
RIterator end = _pData->rend();
for (int i = 1; it != end; ++it, ++i)
if (i == row) return *it;
}
const Type & value(std::size_t row) const
/// Returns the field value in specified row.
{
try
{
return _pData->at(row);
}
catch (std::out_of_range & ex)
{
throw RangeException(ex.what());
}
}
throw RangeException("Invalid row number.");
}
const Type & operator[](std::size_t row) const
/// Returns the field value in specified row.
{
return value(row);
}
const T& operator [] (std::size_t row) const
/// Returns the field value in specified row.
{
return value(row);
}
Size rowCount() const
/// Returns number of rows.
{
return _pData->size();
}
Size rowCount() const
/// Returns number of rows.
{
return _pData->size();
}
void reset()
/// Clears and shrinks the storage.
{
Container().swap(*_pData);
}
void reset()
/// Clears the storage.
{
_pData->clear();
}
const std::string & name() const
/// Returns column name.
{
return _metaColumn.name();
}
const std::string& name() const
/// Returns column name.
{
return _metaColumn.name();
}
std::size_t length() const
/// Returns column maximum length.
{
return _metaColumn.length();
}
std::size_t length() const
/// Returns column maximum length.
{
return _metaColumn.length();
}
std::size_t precision() const
/// Returns column precision.
/// Valid for floating point fields only (zero for other data types).
{
return _metaColumn.precision();
}
std::size_t precision() const
/// Returns column precision.
/// Valid for floating point fields only (zero for other data types).
{
return _metaColumn.precision();
}
std::size_t position() const
/// Returns column position.
{
return _metaColumn.position();
}
std::size_t position() const
/// Returns column position.
{
return _metaColumn.position();
}
MetaColumn::ColumnDataType type() const
/// Returns column type.
{
return _metaColumn.type();
}
MetaColumn::ColumnDataType type() const
/// Returns column type.
{
return _metaColumn.type();
}
Iterator begin() const
/// Returns iterator pointing to the beginning of data storage vector.
{
return _pData->begin();
}
Iterator begin() const
/// Returns iterator pointing to the beginning of data storage vector.
{
return _pData->begin();
}
Iterator end() const
/// Returns iterator pointing to the end of data storage vector.
{
return _pData->end();
}
Iterator end() const
/// Returns iterator pointing to the end of data storage vector.
{
return _pData->end();
}
private:
Column();
private:
Column();
MetaColumn _metaColumn;
ContainerPtr _pData;
};
MetaColumn _metaColumn;
ContainerPtr _pData;
};
template <>
class Column<std::vector<bool>>
/// The std::vector<bool> specialization for the Column class.
///
/// This specialization is necessary due to the nature of std::vector<bool>.
/// For details, see the standard library implementation of vector<bool>
/// or
/// S. Meyers: "Effective STL" (Copyright Addison-Wesley 2001),
/// Item 18: "Avoid using vector<bool>."
///
/// The workaround employed here is using deque<bool> as an
/// internal "companion" container kept in sync with the vector<bool>
/// column data.
{
public:
typedef std::vector<bool> Container;
typedef Poco::SharedPtr<Container> ContainerPtr;
typedef Container::const_iterator Iterator;
typedef Container::const_reverse_iterator RIterator;
typedef Container::size_type Size;
Column(const MetaColumn & metaColumn, Container * pData) : _metaColumn(metaColumn), _pData(pData)
/// Creates the Column.
{
poco_check_ptr(_pData);
_deque.assign(_pData->begin(), _pData->end());
}
Column(const Column & col) : _metaColumn(col._metaColumn), _pData(col._pData)
/// Creates the Column.
{
_deque.assign(_pData->begin(), _pData->end());
}
~Column()
/// Destroys the Column.
{
}
Column & operator=(const Column & col)
/// Assignment operator.
{
Column tmp(col);
swap(tmp);
return *this;
}
void swap(Column & other)
/// Swaps the column with another one.
{
using std::swap;
swap(_metaColumn, other._metaColumn);
swap(_pData, other._pData);
swap(_deque, other._deque);
}
Container & data()
/// Returns reference to contained data.
{
return *_pData;
}
const bool & value(std::size_t row) const
/// Returns the field value in specified row.
{
if (_deque.size() < _pData->size())
_deque.resize(_pData->size());
try
{
return _deque.at(row) = _pData->at(row);
}
catch (std::out_of_range & ex)
{
throw RangeException(ex.what());
}
}
const bool & operator[](std::size_t row) const
/// Returns the field value in specified row.
{
return value(row);
}
Size rowCount() const
/// Returns number of rows.
{
return _pData->size();
}
void reset()
/// Clears and shrinks the storage.
{
Container().swap(*_pData);
_deque.clear();
}
const std::string & name() const
/// Returns column name.
{
return _metaColumn.name();
}
std::size_t length() const
/// Returns column maximum length.
{
return _metaColumn.length();
}
std::size_t precision() const
/// Returns column precision.
/// Valid for floating point fields only (zero for other data types).
{
return _metaColumn.precision();
}
std::size_t position() const
/// Returns column position.
{
return _metaColumn.position();
}
MetaColumn::ColumnDataType type() const
/// Returns column type.
{
return _metaColumn.type();
}
Iterator begin() const
/// Returns iterator pointing to the beginning of data storage vector.
{
return _pData->begin();
}
Iterator end() const
/// Returns iterator pointing to the end of data storage vector.
{
return _pData->end();
}
private:
Column();
MetaColumn _metaColumn;
ContainerPtr _pData;
mutable std::deque<bool> _deque;
};
template <class T>
class Column<std::list<T>>
/// Column specialization for std::list
{
public:
typedef std::list<T> Container;
typedef Poco::SharedPtr<Container> ContainerPtr;
typedef typename Container::const_iterator Iterator;
typedef typename Container::const_reverse_iterator RIterator;
typedef typename Container::size_type Size;
Column(const MetaColumn & metaColumn, std::list<T> * pData) : _metaColumn(metaColumn), _pData(pData)
/// Creates the Column.
{
poco_check_ptr(_pData);
}
Column(const Column & col) : _metaColumn(col._metaColumn), _pData(col._pData)
/// Creates the Column.
{
}
~Column()
/// Destroys the Column.
{
}
Column & operator=(const Column & col)
/// Assignment operator.
{
Column tmp(col);
swap(tmp);
return *this;
}
void swap(Column & other)
/// Swaps the column with another one.
{
using std::swap;
swap(_metaColumn, other._metaColumn);
swap(_pData, other._pData);
}
Container & data()
/// Returns reference to contained data.
{
return *_pData;
}
const T & value(std::size_t row) const
/// Returns the field value in specified row.
/// This is the std::list specialization and std::list
/// is not the optimal solution for cases where random
/// access is needed.
/// However, to allow for compatibility with other
/// containers, this functionality is provided here.
/// To alleviate the problem, an effort is made
/// to start iteration from beginning or end,
/// depending on the position requested.
{
if (row <= (std::size_t)(_pData->size() / 2))
{
Iterator it = _pData->begin();
Iterator end = _pData->end();
for (int i = 0; it != end; ++it, ++i)
if (i == row)
return *it;
}
else
{
row = _pData->size() - row;
RIterator it = _pData->rbegin();
RIterator end = _pData->rend();
for (int i = 1; it != end; ++it, ++i)
if (i == row)
return *it;
}
throw RangeException("Invalid row number.");
}
const T & operator[](std::size_t row) const
/// Returns the field value in specified row.
{
return value(row);
}
Size rowCount() const
/// Returns number of rows.
{
return _pData->size();
}
void reset()
/// Clears the storage.
{
_pData->clear();
}
const std::string & name() const
/// Returns column name.
{
return _metaColumn.name();
}
std::size_t length() const
/// Returns column maximum length.
{
return _metaColumn.length();
}
std::size_t precision() const
/// Returns column precision.
/// Valid for floating point fields only (zero for other data types).
{
return _metaColumn.precision();
}
std::size_t position() const
/// Returns column position.
{
return _metaColumn.position();
}
MetaColumn::ColumnDataType type() const
/// Returns column type.
{
return _metaColumn.type();
}
Iterator begin() const
/// Returns iterator pointing to the beginning of data storage vector.
{
return _pData->begin();
}
Iterator end() const
/// Returns iterator pointing to the end of data storage vector.
{
return _pData->end();
}
private:
Column();
MetaColumn _metaColumn;
ContainerPtr _pData;
};
template <typename C>
inline void swap(Column<C> & c1, Column<C> & c2)
{
c1.swap(c2);
}
template <typename C>
inline void swap(Column<C>& c1, Column<C>& c2)
{
c1.swap(c2);
}
} } // namespace Poco::Data
} // namespace Poco::Data
#endif // Data_Column_INCLUDED

View File

@ -18,39 +18,42 @@
#define Data_Connector_INCLUDED
#include "Poco/AutoPtr.h"
#include "Poco/Data/Data.h"
#include "Poco/Data/SessionImpl.h"
#include "Poco/AutoPtr.h"
namespace Poco {
namespace Data {
class Data_API Connector
/// A Connector creates SessionImpl objects.
///
/// Every connector library (like the SQLite or the ODBC connector)
/// provides a subclass of this class, an instance of which is
/// registered with the SessionFactory.
namespace Poco
{
namespace Data
{
public:
Connector();
/// Creates the Connector.
virtual ~Connector();
/// Destroys the Connector.
virtual const std::string& name() const = 0;
/// Returns the name associated with this connector.
virtual Poco::AutoPtr<SessionImpl> createSession(const std::string& connectionString,
std::size_t timeout = SessionImpl::LOGIN_TIMEOUT_DEFAULT) = 0;
/// Create a SessionImpl object and initialize it with the given connectionString.
};
} } // namespace Poco::Data
class Data_API Connector
/// A Connector creates SessionImpl objects.
///
/// Every connector library (like the SQLite or the ODBC connector)
/// provides a subclass of this class, an instance of which is
/// registered with the SessionFactory.
{
public:
Connector();
/// Creates the Connector.
virtual ~Connector();
/// Destroys the Connector.
virtual const std::string & name() const = 0;
/// Returns the name associated with this connector.
virtual Poco::AutoPtr<SessionImpl>
createSession(const std::string & connectionString, std::size_t timeout = SessionImpl::LOGIN_TIMEOUT_DEFAULT) = 0;
/// Create a SessionImpl object and initialize it with the given connectionString.
};
}
} // namespace Poco::Data
#endif // Data_Connector_INCLUDED

View File

@ -19,18 +19,21 @@
#undef max
#include <limits>
#include <cstddef>
#include <limits>
namespace Poco {
namespace Data {
namespace Poco
{
namespace Data
{
static const std::size_t POCO_DATA_INVALID_ROW = std::numeric_limits<std::size_t>::max();
static const std::size_t POCO_DATA_INVALID_ROW = std::numeric_limits<std::size_t>::max();
} } // namespace Poco::Data
}
} // namespace Poco::Data
#endif // Data_Constants_INCLUDED

View File

@ -31,32 +31,20 @@
// Data_API functions as being imported from a DLL, whereas this DLL sees symbols
// defined with this macro as being exported.
//
#if defined(_WIN32) && defined(POCO_DLL)
#if defined(Data_EXPORTS)
#define Data_API __declspec(dllexport)
#else
#define Data_API __declspec(dllimport)
#endif
#endif
#if !defined(Data_API)
#if !defined(POCO_NO_GCC_API_ATTRIBUTE) && defined (__GNUC__) && (__GNUC__ >= 4)
#define Data_API __attribute__ ((visibility ("default")))
#else
#define Data_API
#endif
# if !defined(POCO_NO_GCC_API_ATTRIBUTE) && defined(__GNUC__) && (__GNUC__ >= 4)
# define Data_API __attribute__((visibility("default")))
# else
# define Data_API
# endif
#endif
//
// Automatically link Data library.
//
#if defined(_MSC_VER)
#if !defined(POCO_NO_AUTOMATIC_LIBS) && !defined(Data_EXPORTS)
#pragma comment(lib, "PocoData" POCO_LIB_SUFFIX)
#endif
#endif
#endif // Data_Data_INCLUDED

View File

@ -22,29 +22,32 @@
#include "Poco/Exception.h"
namespace Poco {
namespace Data {
namespace Poco
{
namespace Data
{
POCO_DECLARE_EXCEPTION(Data_API, DataException, Poco::IOException)
POCO_DECLARE_EXCEPTION(Data_API, RowDataMissingException, DataException)
POCO_DECLARE_EXCEPTION(Data_API, UnknownDataBaseException, DataException)
POCO_DECLARE_EXCEPTION(Data_API, UnknownTypeException, DataException)
POCO_DECLARE_EXCEPTION(Data_API, ExecutionException, DataException)
POCO_DECLARE_EXCEPTION(Data_API, BindingException, DataException)
POCO_DECLARE_EXCEPTION(Data_API, ExtractException, DataException)
POCO_DECLARE_EXCEPTION(Data_API, LimitException, DataException)
POCO_DECLARE_EXCEPTION(Data_API, NotSupportedException, DataException)
POCO_DECLARE_EXCEPTION(Data_API, SessionUnavailableException, DataException)
POCO_DECLARE_EXCEPTION(Data_API, SessionPoolExhaustedException, DataException)
POCO_DECLARE_EXCEPTION(Data_API, SessionPoolExistsException, DataException)
POCO_DECLARE_EXCEPTION(Data_API, NoDataException, DataException)
POCO_DECLARE_EXCEPTION(Data_API, LengthExceededException, DataException)
POCO_DECLARE_EXCEPTION(Data_API, ConnectionFailedException, DataException)
POCO_DECLARE_EXCEPTION(Data_API, NotConnectedException, DataException)
POCO_DECLARE_EXCEPTION(Data_API, DataException, Poco::IOException)
POCO_DECLARE_EXCEPTION(Data_API, RowDataMissingException, DataException)
POCO_DECLARE_EXCEPTION(Data_API, UnknownDataBaseException, DataException)
POCO_DECLARE_EXCEPTION(Data_API, UnknownTypeException, DataException)
POCO_DECLARE_EXCEPTION(Data_API, ExecutionException, DataException)
POCO_DECLARE_EXCEPTION(Data_API, BindingException, DataException)
POCO_DECLARE_EXCEPTION(Data_API, ExtractException, DataException)
POCO_DECLARE_EXCEPTION(Data_API, LimitException, DataException)
POCO_DECLARE_EXCEPTION(Data_API, NotSupportedException, DataException)
POCO_DECLARE_EXCEPTION(Data_API, SessionUnavailableException, DataException)
POCO_DECLARE_EXCEPTION(Data_API, SessionPoolExhaustedException, DataException)
POCO_DECLARE_EXCEPTION(Data_API, SessionPoolExistsException, DataException)
POCO_DECLARE_EXCEPTION(Data_API, NoDataException, DataException)
POCO_DECLARE_EXCEPTION(Data_API, LengthExceededException, DataException)
POCO_DECLARE_EXCEPTION(Data_API, ConnectionFailedException, DataException)
POCO_DECLARE_EXCEPTION(Data_API, NotConnectedException, DataException)
} } // namespace Poco::Data
}
} // namespace Poco::Data
#endif // Data_DataException_INCLUDED

View File

@ -23,135 +23,137 @@
#include "Poco/Exception.h"
namespace Poco {
namespace Poco
{
class DateTime;
namespace Dynamic {
namespace Dynamic
{
class Var;
class Var;
}
namespace Data {
class Time;
class Data_API Date
/// Date class wraps a DateTime and exposes date related interface.
/// The purpose of this class is binding/extraction support for date fields.
namespace Data
{
public:
Date();
/// Creates the Date
Date(int year, int month, int day);
/// Creates the Date
Date(const DateTime& dt);
/// Creates the Date from DateTime
~Date();
/// Destroys the Date.
int year() const;
/// Returns the year.
int month() const;
/// Returns the month.
int day() const;
/// Returns the day.
void assign(int year, int month, int day);
/// Assigns date.
Date& operator = (const Date& d);
/// Assignment operator for Date.
Date& operator = (const DateTime& dt);
/// Assignment operator for DateTime.
Date& operator = (const Poco::Dynamic::Var& var);
/// Assignment operator for Var.
bool operator == (const Date& date) const;
/// Equality operator.
bool operator != (const Date& date) const;
/// Inequality operator.
bool operator < (const Date& date) const;
/// Less then operator.
bool operator > (const Date& date) const;
/// Greater then operator.
private:
int _year;
int _month;
int _day;
};
//
// inlines
//
inline int Date::year() const
{
return _year;
class Time;
class Data_API Date
/// Date class wraps a DateTime and exposes date related interface.
/// The purpose of this class is binding/extraction support for date fields.
{
public:
Date();
/// Creates the Date
Date(int year, int month, int day);
/// Creates the Date
Date(const DateTime & dt);
/// Creates the Date from DateTime
~Date();
/// Destroys the Date.
int year() const;
/// Returns the year.
int month() const;
/// Returns the month.
int day() const;
/// Returns the day.
void assign(int year, int month, int day);
/// Assigns date.
Date & operator=(const Date & d);
/// Assignment operator for Date.
Date & operator=(const DateTime & dt);
/// Assignment operator for DateTime.
Date & operator=(const Poco::Dynamic::Var & var);
/// Assignment operator for Var.
bool operator==(const Date & date) const;
/// Equality operator.
bool operator!=(const Date & date) const;
/// Inequality operator.
bool operator<(const Date & date) const;
/// Less then operator.
bool operator>(const Date & date) const;
/// Greater then operator.
private:
int _year;
int _month;
int _day;
};
//
// inlines
//
inline int Date::year() const
{
return _year;
}
inline int Date::month() const
{
return _month;
}
inline int Date::day() const
{
return _day;
}
inline Date & Date::operator=(const Date & d)
{
assign(d.year(), d.month(), d.day());
return *this;
}
inline Date & Date::operator=(const DateTime & dt)
{
assign(dt.year(), dt.month(), dt.day());
return *this;
}
inline bool Date::operator==(const Date & date) const
{
return _year == date.year() && _month == date.month() && _day == date.day();
}
inline bool Date::operator!=(const Date & date) const
{
return !(*this == date);
}
inline bool Date::operator>(const Date & date) const
{
return !(*this == date) && !(*this < date);
}
}
inline int Date::month() const
{
return _month;
}
inline int Date::day() const
{
return _day;
}
inline Date& Date::operator = (const Date& d)
{
assign(d.year(), d.month(), d.day());
return *this;
}
inline Date& Date::operator = (const DateTime& dt)
{
assign(dt.year(), dt.month(), dt.day());
return *this;
}
inline bool Date::operator == (const Date& date) const
{
return _year == date.year() &&
_month == date.month() &&
_day == date.day();
}
inline bool Date::operator != (const Date& date) const
{
return !(*this == date);
}
inline bool Date::operator > (const Date& date) const
{
return !(*this == date) && !(*this < date);
}
} } // namespace Poco::Data
} // namespace Poco::Data
//
@ -159,67 +161,51 @@ inline bool Date::operator > (const Date& date) const
//
namespace Poco {
namespace Dynamic {
template <>
class VarHolderImpl<Poco::Data::Date>: public VarHolder
namespace Poco
{
namespace Dynamic
{
public:
VarHolderImpl(const Poco::Data::Date& val): _val(val)
{
}
~VarHolderImpl()
{
}
const std::type_info& type() const
{
return typeid(Poco::Data::Date);
}
void convert(Poco::Timestamp& val) const
{
DateTime dt;
dt.assign(_val.year(), _val.month(), _val.day());
val = dt.timestamp();
}
void convert(Poco::DateTime& val) const
{
val.assign(_val.year(), _val.month(), _val.day());
}
void convert(Poco::LocalDateTime& val) const
{
val.assign(_val.year(), _val.month(), _val.day());
}
void convert(std::string& val) const
{
DateTime dt(_val.year(), _val.month(), _val.day());
val = DateTimeFormatter::format(dt, "%Y/%m/%d");
}
VarHolder* clone(Placeholder<VarHolder>* pVarHolder = 0) const
{
return cloneHolder(pVarHolder, _val);
}
const Poco::Data::Date& value() const
{
return _val;
}
private:
VarHolderImpl();
Poco::Data::Date _val;
};
} } // namespace Poco::Dynamic
template <>
class VarHolderImpl<Poco::Data::Date> : public VarHolder
{
public:
VarHolderImpl(const Poco::Data::Date & val) : _val(val) { }
~VarHolderImpl() { }
const std::type_info & type() const { return typeid(Poco::Data::Date); }
void convert(Poco::Timestamp & val) const
{
DateTime dt;
dt.assign(_val.year(), _val.month(), _val.day());
val = dt.timestamp();
}
void convert(Poco::DateTime & val) const { val.assign(_val.year(), _val.month(), _val.day()); }
void convert(Poco::LocalDateTime & val) const { val.assign(_val.year(), _val.month(), _val.day()); }
void convert(std::string & val) const
{
DateTime dt(_val.year(), _val.month(), _val.day());
val = DateTimeFormatter::format(dt, "%Y/%m/%d");
}
VarHolder * clone(Placeholder<VarHolder> * pVarHolder = 0) const { return cloneHolder(pVarHolder, _val); }
const Poco::Data::Date & value() const { return _val; }
private:
VarHolderImpl();
Poco::Data::Date _val;
};
}
} // namespace Poco::Dynamic
#endif // Data_Date_INCLUDED

View File

@ -24,24 +24,32 @@
#include "Poco/Dynamic/Var.h"
namespace Poco {
namespace Data {
namespace Poco
{
namespace Data
{
class Date;
class Time;
class Date;
class Time;
} } // namespace Poco::Data
}
} // namespace Poco::Data
namespace Poco {
namespace Dynamic {
namespace Poco
{
namespace Dynamic
{
template <> Data_API Var::operator Poco::Data::Date () const;
template <> Data_API Var::operator Poco::Data::Time () const;
template <>
Data_API Var::operator Poco::Data::Date() const;
template <>
Data_API Var::operator Poco::Data::Time() const;
} } // namespace Poco::Dynamic
}
} // namespace Poco::Dynamic
#endif // Data_DynamicDateTime_INCLUDED

View File

@ -23,23 +23,32 @@
#include "Poco/Dynamic/Var.h"
namespace Poco {
namespace Data {
namespace Poco
{
namespace Data
{
template <typename T> class LOB;
typedef LOB<unsigned char> BLOB;
typedef LOB<char> CLOB;
template <typename T>
class LOB;
typedef LOB<unsigned char> BLOB;
typedef LOB<char> CLOB;
} } // namespace Poco::Data
}
} // namespace Poco::Data
namespace Poco {
namespace Dynamic {
namespace Poco
{
namespace Dynamic
{
template <> Data_API Var::operator Poco::Data::CLOB () const;
template <> Data_API Var::operator Poco::Data::BLOB () const;
template <>
Data_API Var::operator Poco::Data::CLOB() const;
template <>
Data_API Var::operator Poco::Data::BLOB() const;
} } // namespace Poco::Dynamic
}
} // namespace Poco::Dynamic
#endif // Data_DynamicLOB_INCLUDED

File diff suppressed because it is too large Load Diff

View File

@ -18,203 +18,196 @@
#define Data_LOB_INCLUDED
#include <algorithm>
#include <vector>
#include "Poco/Data/Data.h"
#include "Poco/SharedPtr.h"
#include "Poco/Dynamic/VarHolder.h"
#include "Poco/Exception.h"
#include <vector>
#include <algorithm>
#include "Poco/SharedPtr.h"
namespace Poco {
namespace Data {
template <typename T>
class LOB
/// Representation of a Large OBject.
///
/// A LOB can hold arbitrary data.
/// The maximum size depends on the underlying database.
///
/// The LOBInputStream and LOBOutputStream classes provide
/// a convenient way to access the data in a LOB.
namespace Poco
{
public:
typedef typename std::vector<T>::const_iterator Iterator;
typedef T ValueType;
typedef typename std::vector<T> Container;
typedef Poco::SharedPtr<Container> ContentPtr;
LOB(): _pContent(new std::vector<T>())
/// Creates an empty LOB.
{
}
LOB(const std::vector<T>& content):
_pContent(new std::vector<T>(content))
/// Creates the LOB, content is deep-copied.
{
}
LOB(const T* const pContent, std::size_t size):
_pContent(new std::vector<T>(pContent, pContent + size))
/// Creates the LOB by deep-copying pContent.
{
}
LOB(const std::basic_string<T>& content):
_pContent(new std::vector<T>(content.begin(), content.end()))
/// Creates a LOB from a string.
{
}
LOB(const LOB& other): _pContent(other._pContent)
/// Creates a LOB by copying another one.
{
}
~LOB()
/// Destroys the LOB.
{
}
LOB& operator = (const LOB& other)
/// Assignment operator.
{
LOB tmp(other);
swap(tmp);
return *this;
}
bool operator == (const LOB& other) const
/// Compares for equality LOB by value.
{
return *_pContent == *other._pContent;
}
bool operator != (const LOB& other) const
/// Compares for inequality LOB by value.
{
return *_pContent != *other._pContent;
}
void swap(LOB& other)
/// Swaps the LOB with another one.
{
using std::swap;
swap(_pContent, other._pContent);
}
const std::vector<T>& content() const
/// Returns the content.
{
return *_pContent;
}
const T* rawContent() const
/// Returns the raw content.
///
/// If the LOB is empty, returns NULL.
{
if (_pContent->empty())
return 0;
else
return &(*_pContent)[0];
}
void assignVal(std::size_t count, const T& val)
/// Assigns raw content to internal storage.
{
ContentPtr tmp = new Container(count, val);
_pContent.swap(tmp);
}
void assignRaw(const T* ptr, std::size_t count)
/// Assigns raw content to internal storage.
{
poco_assert_dbg (ptr);
LOB tmp(ptr, count);
swap(tmp);
}
void appendRaw(const T* pChar, std::size_t count)
/// Assigns raw content to internal storage.
{
poco_assert_dbg (pChar);
_pContent->insert(_pContent->end(), pChar, pChar+count);
}
void clear(bool doCompact = false)
/// Clears the content of the blob.
/// If doCompact is true, trims the excess capacity.
{
_pContent->clear();
if (doCompact) compact();
}
void compact()
/// Trims the internal storage excess capacity.
{
std::vector<T>(*_pContent).swap(*_pContent);
}
Iterator begin() const
{
return _pContent->begin();
}
Iterator end() const
{
return _pContent->end();
}
std::size_t size() const
/// Returns the size of the LOB in bytes.
{
return static_cast<std::size_t>(_pContent->size());
}
private:
ContentPtr _pContent;
};
typedef LOB<unsigned char> BLOB;
typedef LOB<char> CLOB;
//
// inlines
//
template <typename T>
inline void swap(LOB<T>& b1, LOB<T>& b2)
namespace Data
{
b1.swap(b2);
template <typename T>
class LOB
/// Representation of a Large OBject.
///
/// A LOB can hold arbitrary data.
/// The maximum size depends on the underlying database.
///
/// The LOBInputStream and LOBOutputStream classes provide
/// a convenient way to access the data in a LOB.
{
public:
typedef typename std::vector<T>::const_iterator Iterator;
typedef T ValueType;
typedef typename std::vector<T> Container;
typedef Poco::SharedPtr<Container> ContentPtr;
LOB() : _pContent(new std::vector<T>())
/// Creates an empty LOB.
{
}
LOB(const std::vector<T> & content) : _pContent(new std::vector<T>(content))
/// Creates the LOB, content is deep-copied.
{
}
LOB(const T * const pContent, std::size_t size) : _pContent(new std::vector<T>(pContent, pContent + size))
/// Creates the LOB by deep-copying pContent.
{
}
LOB(const std::basic_string<T> & content) : _pContent(new std::vector<T>(content.begin(), content.end()))
/// Creates a LOB from a string.
{
}
LOB(const LOB & other) : _pContent(other._pContent)
/// Creates a LOB by copying another one.
{
}
~LOB()
/// Destroys the LOB.
{
}
LOB & operator=(const LOB & other)
/// Assignment operator.
{
LOB tmp(other);
swap(tmp);
return *this;
}
bool operator==(const LOB & other) const
/// Compares for equality LOB by value.
{
return *_pContent == *other._pContent;
}
bool operator!=(const LOB & other) const
/// Compares for inequality LOB by value.
{
return *_pContent != *other._pContent;
}
void swap(LOB & other)
/// Swaps the LOB with another one.
{
using std::swap;
swap(_pContent, other._pContent);
}
const std::vector<T> & content() const
/// Returns the content.
{
return *_pContent;
}
const T * rawContent() const
/// Returns the raw content.
///
/// If the LOB is empty, returns NULL.
{
if (_pContent->empty())
return 0;
else
return &(*_pContent)[0];
}
void assignVal(std::size_t count, const T & val)
/// Assigns raw content to internal storage.
{
ContentPtr tmp = new Container(count, val);
_pContent.swap(tmp);
}
void assignRaw(const T * ptr, std::size_t count)
/// Assigns raw content to internal storage.
{
poco_assert_dbg(ptr);
LOB tmp(ptr, count);
swap(tmp);
}
void appendRaw(const T * pChar, std::size_t count)
/// Assigns raw content to internal storage.
{
poco_assert_dbg(pChar);
_pContent->insert(_pContent->end(), pChar, pChar + count);
}
void clear(bool doCompact = false)
/// Clears the content of the blob.
/// If doCompact is true, trims the excess capacity.
{
_pContent->clear();
if (doCompact)
compact();
}
void compact()
/// Trims the internal storage excess capacity.
{
std::vector<T>(*_pContent).swap(*_pContent);
}
Iterator begin() const { return _pContent->begin(); }
Iterator end() const { return _pContent->end(); }
std::size_t size() const
/// Returns the size of the LOB in bytes.
{
return static_cast<std::size_t>(_pContent->size());
}
private:
ContentPtr _pContent;
};
typedef LOB<unsigned char> BLOB;
typedef LOB<char> CLOB;
//
// inlines
//
template <typename T>
inline void swap(LOB<T> & b1, LOB<T> & b2)
{
b1.swap(b2);
}
}
} } // namespace Poco::Data
} // namespace Poco::Data
namespace std
{
template<>
inline void swap<Poco::Data::BLOB>(Poco::Data::BLOB& b1,
Poco::Data::BLOB& b2)
/// Full template specalization of std:::swap for BLOB
{
b1.swap(b2);
}
template <>
inline void swap<Poco::Data::BLOB>(Poco::Data::BLOB & b1, Poco::Data::BLOB & b2)
/// Full template specalization of std:::swap for BLOB
{
b1.swap(b2);
}
template<>
inline void swap<Poco::Data::CLOB>(Poco::Data::CLOB& c1,
Poco::Data::CLOB& c2)
/// Full template specalization of std:::swap for CLOB
{
c1.swap(c2);
}
template <>
inline void swap<Poco::Data::CLOB>(Poco::Data::CLOB & c1, Poco::Data::CLOB & c2)
/// Full template specalization of std:::swap for CLOB
{
c1.swap(c2);
}
}
@ -223,87 +216,58 @@ namespace std
//
namespace Poco {
namespace Dynamic {
template <>
class VarHolderImpl<Poco::Data::BLOB>: public VarHolder
namespace Poco
{
public:
VarHolderImpl(const Poco::Data::BLOB& val): _val(val)
{
}
~VarHolderImpl()
{
}
const std::type_info& type() const
{
return typeid(Poco::Data::BLOB);
}
void convert(std::string& val) const
{
val.assign(_val.begin(), _val.end());
}
VarHolder* clone(Placeholder<VarHolder>* pVarHolder = 0) const
{
return cloneHolder(pVarHolder, _val);
}
const Poco::Data::BLOB& value() const
{
return _val;
}
private:
VarHolderImpl();
Poco::Data::BLOB _val;
};
template <>
class VarHolderImpl<Poco::Data::CLOB>: public VarHolder
namespace Dynamic
{
public:
VarHolderImpl(const Poco::Data::CLOB& val): _val(val)
{
}
~VarHolderImpl()
{
}
const std::type_info& type() const
{
return typeid(Poco::Data::CLOB);
}
void convert(std::string& val) const
{
val.assign(_val.begin(), _val.end());
}
VarHolder* clone(Placeholder<VarHolder>* pVarHolder = 0) const
{
return cloneHolder(pVarHolder, _val);
}
const Poco::Data::CLOB& value() const
{
return _val;
}
private:
VarHolderImpl();
Poco::Data::CLOB _val;
};
} } // namespace Poco::Dynamic
template <>
class VarHolderImpl<Poco::Data::BLOB> : public VarHolder
{
public:
VarHolderImpl(const Poco::Data::BLOB & val) : _val(val) { }
~VarHolderImpl() { }
const std::type_info & type() const { return typeid(Poco::Data::BLOB); }
void convert(std::string & val) const { val.assign(_val.begin(), _val.end()); }
VarHolder * clone(Placeholder<VarHolder> * pVarHolder = 0) const { return cloneHolder(pVarHolder, _val); }
const Poco::Data::BLOB & value() const { return _val; }
private:
VarHolderImpl();
Poco::Data::BLOB _val;
};
template <>
class VarHolderImpl<Poco::Data::CLOB> : public VarHolder
{
public:
VarHolderImpl(const Poco::Data::CLOB & val) : _val(val) { }
~VarHolderImpl() { }
const std::type_info & type() const { return typeid(Poco::Data::CLOB); }
void convert(std::string & val) const { val.assign(_val.begin(), _val.end()); }
VarHolder * clone(Placeholder<VarHolder> * pVarHolder = 0) const { return cloneHolder(pVarHolder, _val); }
const Poco::Data::CLOB & value() const { return _val; }
private:
VarHolderImpl();
Poco::Data::CLOB _val;
};
}
} // namespace Poco::Dynamic
#endif // Data_LOB_INCLUDED

View File

@ -18,133 +18,132 @@
#define Data_LOBStream_INCLUDED
#include "Poco/Foundation.h"
#include "Poco/UnbufferedStreamBuf.h"
#include "Poco/Data/LOB.h"
#include <istream>
#include <ostream>
#include "Poco/Data/LOB.h"
#include "Poco/Foundation.h"
#include "Poco/UnbufferedStreamBuf.h"
namespace Poco {
namespace Data {
template <typename T>
class LOBStreamBuf: public BasicUnbufferedStreamBuf<T, std::char_traits<T> >
/// This is the streambuf class used for reading from and writing to a LOB.
namespace Poco
{
public:
LOBStreamBuf(LOB<T>& lob): _lob(lob), _it(_lob.begin())
/// Creates LOBStreamBuf.
{
}
~LOBStreamBuf()
/// Destroys LOBStreamBuf.
{
}
protected:
typedef std::char_traits<T> TraitsType;
typedef BasicUnbufferedStreamBuf<T, TraitsType> BaseType;
typename BaseType::int_type readFromDevice()
{
if (_it != _lob.end())
return BaseType::charToInt(*_it++);
else
return -1;
}
typename BaseType::int_type writeToDevice(T c)
{
_lob.appendRaw(&c, 1);
return 1;
}
private:
LOB<T>& _lob;
typename LOB<T>::Iterator _it;
};
template <typename T>
class LOBIOS: public virtual std::ios
/// The base class for LOBInputStream and
/// LOBOutputStream.
///
/// This class is needed to ensure the correct initialization
/// order of the stream buffer and base classes.
namespace Data
{
public:
LOBIOS(LOB<T>& lob, openmode mode): _buf(lob)
/// Creates the LOBIOS with the given LOB.
{
poco_ios_init(&_buf);
}
~LOBIOS()
/// Destroys the LOBIOS.
{
}
LOBStreamBuf<T>* rdbuf()
/// Returns a pointer to the internal LOBStreamBuf.
{
return &_buf;
}
protected:
LOBStreamBuf<T> _buf;
};
template <typename T>
class LOBOutputStream: public LOBIOS<T>, public std::basic_ostream<T, std::char_traits<T> >
/// An output stream for writing to a LOB.
{
public:
LOBOutputStream(LOB<T>& lob):
LOBIOS<T>(lob, std::ios::out),
std::ostream(LOBIOS<T>::rdbuf())
/// Creates the LOBOutputStream with the given LOB.
{
}
~LOBOutputStream()
/// Destroys the LOBOutputStream.
{
}
};
template <typename T>
class LOBStreamBuf : public BasicUnbufferedStreamBuf<T, std::char_traits<T>>
/// This is the streambuf class used for reading from and writing to a LOB.
{
public:
LOBStreamBuf(LOB<T> & lob) : _lob(lob), _it(_lob.begin())
/// Creates LOBStreamBuf.
{
}
template <typename T>
class LOBInputStream: public LOBIOS<T>, public std::basic_istream<T, std::char_traits<T> >
/// An input stream for reading from a LOB.
{
public:
LOBInputStream(LOB<T>& lob):
LOBIOS<T>(lob, std::ios::in),
std::istream(LOBIOS<T>::rdbuf())
/// Creates the LOBInputStream with the given LOB.
{
}
~LOBStreamBuf()
/// Destroys LOBStreamBuf.
{
}
~LOBInputStream()
/// Destroys the LOBInputStream.
{
}
};
protected:
typedef std::char_traits<T> TraitsType;
typedef BasicUnbufferedStreamBuf<T, TraitsType> BaseType;
typename BaseType::int_type readFromDevice()
{
if (_it != _lob.end())
return BaseType::charToInt(*_it++);
else
return -1;
}
typename BaseType::int_type writeToDevice(T c)
{
_lob.appendRaw(&c, 1);
return 1;
}
private:
LOB<T> & _lob;
typename LOB<T>::Iterator _it;
};
typedef LOBOutputStream<unsigned char> BLOBOutputStream;
typedef LOBOutputStream<char> CLOBOutputStream;
template <typename T>
class LOBIOS : public virtual std::ios
/// The base class for LOBInputStream and
/// LOBOutputStream.
///
/// This class is needed to ensure the correct initialization
/// order of the stream buffer and base classes.
{
public:
LOBIOS(LOB<T> & lob, openmode mode) : _buf(lob)
/// Creates the LOBIOS with the given LOB.
{
poco_ios_init(&_buf);
}
typedef LOBInputStream<unsigned char> BLOBInputStream;
typedef LOBInputStream<char> CLOBInputStream;
~LOBIOS()
/// Destroys the LOBIOS.
{
}
} } // namespace Poco::Data
LOBStreamBuf<T> * rdbuf()
/// Returns a pointer to the internal LOBStreamBuf.
{
return &_buf;
}
protected:
LOBStreamBuf<T> _buf;
};
template <typename T>
class LOBOutputStream : public LOBIOS<T>, public std::basic_ostream<T, std::char_traits<T>>
/// An output stream for writing to a LOB.
{
public:
LOBOutputStream(LOB<T> & lob) : LOBIOS<T>(lob, std::ios::out), std::ostream(LOBIOS<T>::rdbuf())
/// Creates the LOBOutputStream with the given LOB.
{
}
~LOBOutputStream()
/// Destroys the LOBOutputStream.
{
}
};
template <typename T>
class LOBInputStream : public LOBIOS<T>, public std::basic_istream<T, std::char_traits<T>>
/// An input stream for reading from a LOB.
{
public:
LOBInputStream(LOB<T> & lob) : LOBIOS<T>(lob, std::ios::in), std::istream(LOBIOS<T>::rdbuf())
/// Creates the LOBInputStream with the given LOB.
{
}
~LOBInputStream()
/// Destroys the LOBInputStream.
{
}
};
typedef LOBOutputStream<unsigned char> BLOBOutputStream;
typedef LOBOutputStream<char> CLOBOutputStream;
typedef LOBInputStream<unsigned char> BLOBInputStream;
typedef LOBInputStream<char> CLOBInputStream;
}
} // namespace Poco::Data
#endif // Data_LOBStream_INCLUDED

View File

@ -21,93 +21,92 @@
#include "Poco/Data/Data.h"
namespace Poco {
namespace Data {
class Data_API Limit
/// Limit stores information how many rows a query should return.
namespace Poco
{
public:
typedef Poco::UInt32 SizeT;
enum Type
{
LIMIT_UNLIMITED = ~((SizeT) 0)
};
Limit(SizeT value, bool hardLimit = false, bool isLowerLimit = false);
/// Creates the Limit.
///
/// Value contains the upper row hint, if hardLimit is set to true, the limit acts as a hard
/// border, ie. every query must return exactly value rows, returning more than value objects will throw an exception!
/// LowerLimits always act as hard-limits!
///
/// A value of LIMIT_UNLIMITED disables the limit.
~Limit();
/// Destroys the Limit.
SizeT value() const;
/// Returns the value of the limit
bool isHardLimit() const;
/// Returns true if the limit is a hard limit.
bool isLowerLimit() const;
/// Returns true if the limit is a lower limit, otherwise it is an upperLimit
bool operator == (const Limit& other) const;
/// Equality operator.
bool operator != (const Limit& other) const;
/// Inequality operator.
private:
SizeT _value;
bool _hardLimit;
bool _isLowerLimit;
};
//
// inlines
//
inline Poco::UInt32 Limit::value() const
namespace Data
{
return _value;
class Data_API Limit
/// Limit stores information how many rows a query should return.
{
public:
typedef Poco::UInt32 SizeT;
enum Type
{
LIMIT_UNLIMITED = ~((SizeT)0)
};
Limit(SizeT value, bool hardLimit = false, bool isLowerLimit = false);
/// Creates the Limit.
///
/// Value contains the upper row hint, if hardLimit is set to true, the limit acts as a hard
/// border, ie. every query must return exactly value rows, returning more than value objects will throw an exception!
/// LowerLimits always act as hard-limits!
///
/// A value of LIMIT_UNLIMITED disables the limit.
~Limit();
/// Destroys the Limit.
SizeT value() const;
/// Returns the value of the limit
bool isHardLimit() const;
/// Returns true if the limit is a hard limit.
bool isLowerLimit() const;
/// Returns true if the limit is a lower limit, otherwise it is an upperLimit
bool operator==(const Limit & other) const;
/// Equality operator.
bool operator!=(const Limit & other) const;
/// Inequality operator.
private:
SizeT _value;
bool _hardLimit;
bool _isLowerLimit;
};
//
// inlines
//
inline Poco::UInt32 Limit::value() const
{
return _value;
}
inline bool Limit::isHardLimit() const
{
return _hardLimit;
}
inline bool Limit::isLowerLimit() const
{
return _isLowerLimit;
}
inline bool Limit::operator==(const Limit & other) const
{
return other._value == _value && other._hardLimit == _hardLimit && other._isLowerLimit == _isLowerLimit;
}
inline bool Limit::operator!=(const Limit & other) const
{
return other._value != _value || other._hardLimit != _hardLimit || other._isLowerLimit != _isLowerLimit;
}
}
inline bool Limit::isHardLimit() const
{
return _hardLimit;
}
inline bool Limit::isLowerLimit() const
{
return _isLowerLimit;
}
inline bool Limit::operator == (const Limit& other) const
{
return other._value == _value &&
other._hardLimit == _hardLimit &&
other._isLowerLimit == _isLowerLimit;
}
inline bool Limit::operator != (const Limit& other) const
{
return other._value != _value ||
other._hardLimit != _hardLimit ||
other._isLowerLimit != _isLowerLimit;
}
} } // namespace Poco::Data
} // namespace Poco::Data
#endif // Data_Limit_INCLUDED

View File

@ -18,171 +18,175 @@
#define Data_MetaColumn_INCLUDED
#include "Poco/Data/Data.h"
#include <cstddef>
#include "Poco/Data/Data.h"
namespace Poco {
namespace Data {
class Data_API MetaColumn
/// MetaColumn class contains column metadata information.
namespace Poco
{
public:
enum ColumnDataType
{
FDT_BOOL,
FDT_INT8,
FDT_UINT8,
FDT_INT16,
FDT_UINT16,
FDT_INT32,
FDT_UINT32,
FDT_INT64,
FDT_UINT64,
FDT_FLOAT,
FDT_DOUBLE,
FDT_STRING,
FDT_WSTRING,
FDT_BLOB,
FDT_CLOB,
FDT_DATE,
FDT_TIME,
FDT_TIMESTAMP,
FDT_UNKNOWN
};
MetaColumn();
/// Creates the MetaColumn.
explicit MetaColumn(std::size_t position,
const std::string& name = "",
ColumnDataType type = FDT_UNKNOWN,
std::size_t length = 0,
std::size_t precision = 0,
bool nullable = false);
/// Creates the MetaColumn.
virtual ~MetaColumn();
/// Destroys the MetaColumn.
const std::string& name() const;
/// Returns column name.
std::size_t length() const;
/// Returns column maximum length.
std::size_t precision() const;
/// Returns column precision.
/// Valid for floating point fields only
/// (zero for other data types).
std::size_t position() const;
/// Returns column position.
ColumnDataType type() const;
/// Returns column type.
bool isNullable() const;
/// Returns true if column allows null values, false otherwise.
protected:
void setName(const std::string& name);
/// Sets the column name.
void setLength(std::size_t length);
/// Sets the column length.
void setPrecision(std::size_t precision);
/// Sets the column precision.
void setType(ColumnDataType type);
/// Sets the column data type.
void setNullable(bool nullable);
/// Sets the column nullability.
private:
std::string _name;
std::size_t _length;
std::size_t _precision;
std::size_t _position;
ColumnDataType _type;
bool _nullable;
};
///
/// inlines
///
inline const std::string& MetaColumn::name() const
namespace Data
{
return _name;
class Data_API MetaColumn
/// MetaColumn class contains column metadata information.
{
public:
enum ColumnDataType
{
FDT_BOOL,
FDT_INT8,
FDT_UINT8,
FDT_INT16,
FDT_UINT16,
FDT_INT32,
FDT_UINT32,
FDT_INT64,
FDT_UINT64,
FDT_FLOAT,
FDT_DOUBLE,
FDT_STRING,
FDT_WSTRING,
FDT_BLOB,
FDT_CLOB,
FDT_DATE,
FDT_TIME,
FDT_TIMESTAMP,
FDT_UNKNOWN
};
MetaColumn();
/// Creates the MetaColumn.
explicit MetaColumn(
std::size_t position,
const std::string & name = "",
ColumnDataType type = FDT_UNKNOWN,
std::size_t length = 0,
std::size_t precision = 0,
bool nullable = false);
/// Creates the MetaColumn.
virtual ~MetaColumn();
/// Destroys the MetaColumn.
const std::string & name() const;
/// Returns column name.
std::size_t length() const;
/// Returns column maximum length.
std::size_t precision() const;
/// Returns column precision.
/// Valid for floating point fields only
/// (zero for other data types).
std::size_t position() const;
/// Returns column position.
ColumnDataType type() const;
/// Returns column type.
bool isNullable() const;
/// Returns true if column allows null values, false otherwise.
protected:
void setName(const std::string & name);
/// Sets the column name.
void setLength(std::size_t length);
/// Sets the column length.
void setPrecision(std::size_t precision);
/// Sets the column precision.
void setType(ColumnDataType type);
/// Sets the column data type.
void setNullable(bool nullable);
/// Sets the column nullability.
private:
std::string _name;
std::size_t _length;
std::size_t _precision;
std::size_t _position;
ColumnDataType _type;
bool _nullable;
};
///
/// inlines
///
inline const std::string & MetaColumn::name() const
{
return _name;
}
inline std::size_t MetaColumn::length() const
{
return _length;
}
inline std::size_t MetaColumn::precision() const
{
return _precision;
}
inline std::size_t MetaColumn::position() const
{
return _position;
}
inline MetaColumn::ColumnDataType MetaColumn::type() const
{
return _type;
}
inline bool MetaColumn::isNullable() const
{
return _nullable;
}
inline void MetaColumn::setName(const std::string & name)
{
_name = name;
}
inline void MetaColumn::setLength(std::size_t length)
{
_length = length;
}
inline void MetaColumn::setPrecision(std::size_t precision)
{
_precision = precision;
}
inline void MetaColumn::setType(ColumnDataType type)
{
_type = type;
}
inline void MetaColumn::setNullable(bool nullable)
{
_nullable = nullable;
}
}
inline std::size_t MetaColumn::length() const
{
return _length;
}
inline std::size_t MetaColumn::precision() const
{
return _precision;
}
inline std::size_t MetaColumn::position() const
{
return _position;
}
inline MetaColumn::ColumnDataType MetaColumn::type() const
{
return _type;
}
inline bool MetaColumn::isNullable() const
{
return _nullable;
}
inline void MetaColumn::setName(const std::string& name)
{
_name = name;
}
inline void MetaColumn::setLength(std::size_t length)
{
_length = length;
}
inline void MetaColumn::setPrecision(std::size_t precision)
{
_precision = precision;
}
inline void MetaColumn::setType(ColumnDataType type)
{
_type = type;
}
inline void MetaColumn::setNullable(bool nullable)
{
_nullable = nullable;
}
} } // namespace Poco::Data
} // namespace Poco::Data
#endif // Data_MetaColumn_INCLUDED

Some files were not shown because too many files have changed in this diff Show More