├── .github ├── docker-compose.yml ├── script │ ├── build-tdengine-macos.sh │ ├── build-tdengine.bat │ ├── build-tdengine.sh │ └── download-tdengine.ps1 └── workflows │ ├── ci.yml │ └── winext │ ├── action.yml │ ├── build.ps1 │ ├── deps.ps1 │ ├── devpack.ps1 │ ├── devpack_master.ps1 │ ├── getphp.ps1 │ ├── install.ps1 │ ├── release.ps1 │ ├── uploader.ps1 │ └── utils.ps1 ├── .gitignore ├── LICENSE ├── README.md ├── config.m4 ├── config.w32 ├── include ├── ext_taos.h ├── ext_taos_connection.h ├── ext_taos_resource.h ├── ext_taos_statement.h ├── ext_tdengine.h └── tdengine_swoole.h ├── make.ps1 ├── make.sh ├── php_tdengine.h ├── run-tests.ps1 ├── run-tests.sh ├── src ├── ext_taos.cc ├── ext_taos_connection.cc ├── ext_taos_resource.cc └── ext_taos_statement.cc ├── tdengine.cc ├── tdengine.stub.php ├── tdengine_arginfo.h └── tests ├── 01-sync ├── 001-extension-loaded.phpt ├── 002-connect-close.phpt ├── 003-query.phpt ├── 004-prepare.phpt └── 005-prepare-batch.phpt ├── 02-swoole ├── 001-extension-loaded.phpt ├── 002-connect-close.phpt ├── 003-query.phpt ├── 004-prepare.phpt └── 005-prepare-batch.phpt └── include ├── Assert.php ├── include.php └── skip └── skip-no-swoole.php /.github/docker-compose.yml: -------------------------------------------------------------------------------- 1 | version: "3" 2 | services: 3 | tdengine: 4 | restart: always 5 | image: tdengine/tdengine:$TDENGINE_VERSION 6 | hostname: tdengine 7 | container_name: tdengine 8 | privileged: true 9 | ports: 10 | - 6020:6020 11 | - 6030-6042:6030-6042/tcp 12 | - 6030-6042:6030-6042/udp 13 | -------------------------------------------------------------------------------- /.github/script/build-tdengine-macos.sh: -------------------------------------------------------------------------------- 1 | #!/bin/bash 2 | cd /tmp && \ 3 | git clone --recurse -b ver-${TDENGINE_VERSION} --depth=1 https://github.com/taosdata/TDengine.git && \ 4 | cd TDengine && \ 5 | mkdir debug && cd debug && cmake .. && make && make install && \ 6 | echo "rpcForceTcp 1" >> /usr/local/etc/taos/taos.cfg 7 | -------------------------------------------------------------------------------- /.github/script/build-tdengine.bat: -------------------------------------------------------------------------------- 1 | @ECHO OFF 2 | 3 | d: 4 | cd "D:\TDengine" 5 | 6 | mkdir release 7 | 8 | cd release 9 | 10 | call "C:\Program Files (x86)\Microsoft Visual Studio\%VS_VERSION%\Enterprise\VC\Auxiliary\Build\vcvarsall.bat" x64 11 | 12 | cmake .. -G "NMake Makefiles" -DBUILD_JDBC=false -DTD_BUILD_HTTP=false -DTD_BUILD_LUA=false 13 | 14 | nmake 15 | 16 | nmake install 17 | 18 | copy "C:\TDengine\driver\taos.dll" "C:\Windows\System32" 19 | -------------------------------------------------------------------------------- /.github/script/build-tdengine.sh: -------------------------------------------------------------------------------- 1 | #!/bin/bash 2 | cd /tmp && \ 3 | git clone --recurse -b ver-${TDENGINE_VERSION} --depth=1 https://github.com/taosdata/TDengine.git && \ 4 | cd TDengine && \ 5 | mkdir debug && cd debug && cmake .. -DBUILD_JDBC=false -DBUILD_TOOLS=false && make -j && make install && \ 6 | systemctl start taosd 7 | -------------------------------------------------------------------------------- /.github/script/download-tdengine.ps1: -------------------------------------------------------------------------------- 1 | #!/bin/bash 2 | 3 | cd D:\ 4 | 5 | git clone --recurse -b ver-$env:TDENGINE_VERSION --depth=1 https://github.com/taosdata/TDengine.git 6 | -------------------------------------------------------------------------------- /.github/workflows/ci.yml: -------------------------------------------------------------------------------- 1 | name: ci 2 | 3 | on: [push, pull_request] 4 | 5 | jobs: 6 | test-linux: 7 | name: Linux PHP-${{ matrix.php }} TDengine-${{ matrix.tdengine }} Swoole-${{ matrix.swoole }} 8 | runs-on: ubuntu-20.04 9 | 10 | strategy: 11 | fail-fast: false 12 | matrix: 13 | php: [7.4, '8.0', 8.1] 14 | swoole: ['5.0.0', '4.8.10'] 15 | tdengine: ['2.4.0.18'] 16 | exclude: 17 | - php: 7.4 18 | swoole: '5.0.0' 19 | 20 | env: 21 | TDENGINE_VERSION: ${{ matrix.tdengine }} 22 | 23 | steps: 24 | - uses: actions/checkout@v2 25 | 26 | - name: Prepare 27 | run: sudo apt update && sudo apt install -y gcc cmake build-essential git libssl-dev 28 | 29 | - name: Build TDengine 30 | run: sudo -E bash .github/script/build-tdengine.sh 31 | 32 | - name: Setup PHP 33 | uses: shivammathur/setup-php@v2 34 | with: 35 | php-version: ${{ matrix.php }} 36 | tools: pecl 37 | extensions: > 38 | :xdebug 39 | openssl, mbstring, json, sockets, 40 | swoole-swoole/swoole-src@v${{ matrix.swoole }} 41 | env: 42 | SWOOLE_CONFIGURE_OPTS: > 43 | --enable-openssl 44 | --with-openssl-dir=${{ steps.opecssl-dir.outputs.path }} 45 | 46 | - name: Build extension 47 | run: ./make.sh 48 | 49 | - name: Test 50 | run: ./run-tests.sh 51 | 52 | test-tdengine-linux: 53 | name: Linux PHP-${{ matrix.php }} TDengine-${{ matrix.tdengine }} Swoole-${{ matrix.swoole }} 54 | runs-on: ubuntu-20.04 55 | 56 | strategy: 57 | fail-fast: false 58 | matrix: 59 | php: [8.1] 60 | swoole: ['5.0.0'] 61 | tdengine: ['3.0.0.1', '2.6.0.1'] 62 | 63 | env: 64 | TDENGINE_VERSION: ${{ matrix.tdengine }} 65 | 66 | steps: 67 | - uses: actions/checkout@v2 68 | 69 | - name: Prepare 70 | run: sudo apt update && sudo apt install -y gcc cmake build-essential git libssl-dev 71 | 72 | - name: Build TDengine 73 | run: sudo -E bash .github/script/build-tdengine.sh 74 | 75 | - name: Setup PHP 76 | uses: shivammathur/setup-php@v2 77 | with: 78 | php-version: ${{ matrix.php }} 79 | tools: pecl 80 | extensions: > 81 | :xdebug 82 | openssl, mbstring, json, sockets, 83 | swoole-swoole/swoole-src@v${{ matrix.swoole }} 84 | env: 85 | SWOOLE_CONFIGURE_OPTS: > 86 | --enable-openssl 87 | --with-openssl-dir=${{ steps.opecssl-dir.outputs.path }} 88 | 89 | - name: Build extension 90 | run: ./make.sh 91 | 92 | - name: Test 93 | run: ./run-tests.sh 94 | 95 | test-windows: 96 | name: Windows PHP-${{ matrix.php.version }} TDengine-${{ matrix.tdengine }} 97 | runs-on: ${{ matrix.php.runs-on }} 98 | 99 | strategy: 100 | fail-fast: false 101 | matrix: 102 | php: 103 | - version: '8.0' 104 | runs-on: windows-2019 105 | vs: 2019 106 | - version: 8.1 107 | runs-on: windows-2019 108 | vs: 2019 109 | tdengine: ['2.4.0.18'] 110 | 111 | env: 112 | TDENGINE_VERSION: ${{ matrix.tdengine }} 113 | VS_VERSION: ${{ matrix.php.vs }} 114 | 115 | steps: 116 | - uses: actions/checkout@v2 117 | 118 | - name: Download TDengine 119 | run: .github\script\download-tdengine.ps1 120 | 121 | - name: Build TDengine 122 | shell: cmd 123 | run: .github\script\build-tdengine.bat 124 | 125 | - name: Setup PHP 126 | uses: shivammathur/setup-php@v2 127 | with: 128 | php-version: ${{ matrix.php.version }} 129 | tools: pecl 130 | extensions: > 131 | :xdebug 132 | openssl, mbstring, json, sockets, 133 | 134 | - name: Build extension 135 | uses: ./.github/workflows/winext 136 | with: 137 | ext-path: . 138 | tools-path: C:\tools\phpdev 139 | ext-name: tdengine 140 | enable-extension: 1 141 | - name: Test 142 | run: | 143 | php -m 144 | php --ri tdengine 145 | 146 | test-tdengine-windows: 147 | name: Windows PHP-${{ matrix.php.version }} TDengine-${{ matrix.tdengine }} 148 | runs-on: ${{ matrix.php.runs-on }} 149 | 150 | strategy: 151 | fail-fast: false 152 | matrix: 153 | php: 154 | - version: 8.1 155 | runs-on: windows-2019 156 | vs: 2019 157 | tdengine: ['3.0.0.1', '2.6.0.1'] 158 | 159 | env: 160 | TDENGINE_VERSION: ${{ matrix.tdengine }} 161 | VS_VERSION: ${{ matrix.php.vs }} 162 | 163 | steps: 164 | - uses: actions/checkout@v2 165 | 166 | - name: Download TDengine 167 | run: .github\script\download-tdengine.ps1 168 | 169 | - name: Build TDengine 170 | shell: cmd 171 | run: .github\script\build-tdengine.bat 172 | 173 | - name: Setup PHP 174 | uses: shivammathur/setup-php@v2 175 | with: 176 | php-version: ${{ matrix.php.version }} 177 | tools: pecl 178 | extensions: > 179 | :xdebug 180 | openssl, mbstring, json, sockets, 181 | 182 | - name: Build extension 183 | uses: ./.github/workflows/winext 184 | with: 185 | ext-path: . 186 | tools-path: C:\tools\phpdev 187 | ext-name: tdengine 188 | - name: Test 189 | run: | 190 | php -m 191 | php --ri tdengine 192 | 193 | # test-macos: 194 | # name: MacOS PHP-${{ matrix.php }} TDengine-${{ matrix.tdengine }} Swoole-${{ matrix.swoole }} 195 | # runs-on: macos-10.15 196 | 197 | # strategy: 198 | # fail-fast: false 199 | # matrix: 200 | # php: [7.4, '8.0', 8.1] 201 | # swoole: ['4.8.10'] 202 | # tdengine: ['2.4.0.18'] 203 | 204 | # env: 205 | # TDENGINE_VERSION: ${{ matrix.tdengine }} 206 | 207 | # steps: 208 | # - uses: actions/checkout@v2 209 | # - uses: docker-practice/actions-setup-docker@1.0.8 210 | 211 | # - run: echo "127.0.0.1 tdengine" | sudo tee -a /etc/hosts > /dev/null 212 | 213 | # - name: Build TDengine 214 | # run: .github/script/build-tdengine-macos.sh 215 | 216 | # - name: Docker 217 | # run: | 218 | # cd .github 219 | # docker-compose up -d 220 | 221 | # - name: Get Openssl Dir 222 | # id: opecssl-dir 223 | # run: echo "::set-output name=path::$(brew --prefix openssl@1.1)" 224 | 225 | # - name: Setup PHP 226 | # uses: shivammathur/setup-php@v2 227 | # with: 228 | # php-version: ${{ matrix.php }} 229 | # tools: pecl 230 | # extensions: > 231 | # :xdebug 232 | # openssl, mbstring, json, sockets, 233 | # swoole-swoole/swoole-src@v${{ matrix.swoole }} 234 | # env: 235 | # SWOOLE_CONFIGURE_OPTS: > 236 | # --enable-openssl 237 | # --with-openssl-dir=${{ steps.opecssl-dir.outputs.path }} 238 | 239 | # - name: Build extension 240 | # run: | 241 | # phpize 242 | # ./configure --enable-swoole --with-tdengine-dir=/usr/local/Cellar/tdengine/$TDENGINE_VERSION 243 | # make -j 244 | # make install 245 | 246 | # - name: Test 247 | # run: ./run-tests.sh 248 | -------------------------------------------------------------------------------- /.github/workflows/winext/action.yml: -------------------------------------------------------------------------------- 1 | name: 'Windows PHP Extension Build' 2 | description: 'Build windows extension via php offical devpack according to php varient.' 3 | inputs: 4 | max-try: 5 | description: 'max retry for downloading things' 6 | required: false 7 | default: "3" 8 | tools-path: 9 | description: 'downloaded file path' 10 | required: true 11 | default: 'C:\tools\phpdev' 12 | ext-path: 13 | description: 'extension path' 14 | required: false 15 | default: . 16 | conf-args: 17 | description: 'configure options' 18 | required: false 19 | default: "" 20 | ext-name: 21 | description: 'extension name' 22 | required: true 23 | default: "" 24 | deps: 25 | description: 'deps, comma splited' 26 | required: false 27 | default: "" 28 | install: 29 | description: 'if we install it, 0 for not install, 1 for install' 30 | required: false 31 | default: "1" 32 | phpver: 33 | description: 'php version that we used in format like "8.0", or "php" for auto-detect using php in %PATH%' 34 | required: false 35 | default: "php" 36 | phpts: 37 | description: 'php zend ts enabled, 0 for nts, 1 for ts' 38 | required: false 39 | default: "0" 40 | phparch: 41 | description: 'php arch we used, now there are x64 and x86' 42 | required: false 43 | default: "x64" 44 | staging-deps: 45 | description: 'use staging dependencies' 46 | required: false 47 | default: "0" 48 | fix-pickle: 49 | description: 'fix pickle.h definitions duplicates' 50 | required: false 51 | default: "1" 52 | enable-extension: 53 | description: 'enable extension' 54 | required: false 55 | default: "1" 56 | outputs: 57 | vcver: 58 | description: 'max retry for downloading things' 59 | value: "${{ steps.devpack-prepare.outputs.vcver }}" 60 | runs: 61 | using: 'composite' 62 | steps: 63 | - name: Prepare php-sdk-binary-tools 64 | shell: cmd 65 | id: phpsdk-prepare 66 | run: | 67 | IF NOT EXIST ${{ inputs.tools-path }} ( MKDIR ${{ inputs.tools-path }} ) 68 | IF NOT EXIST ${{ inputs.tools-path }}\php-sdk-binary-tools ( 69 | ECHO ::group::Fetching php-sdk-binary-tools from Github 70 | git clone --single-branch --depth=1 https://github.com/Microsoft/php-sdk-binary-tools ${{ inputs.tools-path }}\php-sdk-binary-tools || EXIT /B 1 71 | ECHO ::endgroup:: 72 | ) 73 | git --git-dir=${{ inputs.tools-path }}\php-sdk-binary-tools\.git --work-tree=${{ inputs.tools-path }}\php-sdk-binary-tools pull || EXIT /B 1 74 | 75 | - name: Prepare PHP devel-pack and deps 76 | shell: powershell 77 | id: devpack-prepare 78 | env: 79 | UNIX_COLOR: "1" 80 | run: | 81 | if ("${{ inputs.phpver }}".Equals("php")) { 82 | $PhpBin = "php" 83 | $PhpVer = "" 84 | } else { 85 | $PhpBin = "" 86 | $PhpVer = "${{ inputs.phpver }}" 87 | } 88 | $master = $PhpVer 89 | if ($PhpVer.Equals("8.2")) { 90 | # using master 91 | $master = "master" 92 | $PhpVCVer = "vs16" 93 | } elseif ($PhpVer.Equals("8.1") -Or $PhpVer.Equals("8.0")) { 94 | $PhpVCVer = "vs16" 95 | } else { 96 | $PhpVCVer = "vc15" 97 | } 98 | Write-Host "::set-output name=vcver::$PhpVCVer" 99 | Write-Host "::group::Fetching deps from windows.php.net" 100 | ${{github.action_path}}/deps.ps1 ` 101 | ${{ inputs.deps }} ` 102 | -MaxTry ${{ inputs.max-try }} ` 103 | -ToolsPath ${{ inputs.tools-path }} ` 104 | -PhpBin $PhpBin ` 105 | -PhpVer $master ` 106 | -PhpTs ${{ inputs.phpts }} ` 107 | -PhpArch ${{ inputs.phparch }} ` 108 | -PhpVCVer $PhpVCVer ` 109 | -Staging ${{ inputs.staging-deps }} 110 | $ret = $lastexitcode 111 | Write-Host "::endgroup::" 112 | if (0 -Ne $ret) { 113 | exit 1 114 | } 115 | if ($master.Equals("master")) { 116 | Write-Host "::group::Fetching devel-pack from shivammathur/php-builder-windows" 117 | ${{github.action_path}}/devpack_master.ps1 ` 118 | -MaxTry ${{ inputs.max-try }} ` 119 | -ToolsPath ${{ inputs.tools-path }} ` 120 | -PhpTs ${{ inputs.phpts }} ` 121 | -PhpArch ${{ inputs.phparch }} ` 122 | -PhpVCVer $PhpVCVer 123 | $ret = $lastexitcode 124 | Write-Host "::endgroup::" 125 | } else { 126 | Write-Host "::group::Fetching devel-pack from windows.php.net" 127 | ${{github.action_path}}/devpack.ps1 ` 128 | -MaxTry ${{ inputs.max-try }} ` 129 | -ToolsPath ${{ inputs.tools-path }} ` 130 | -PhpBin $PhpBin ` 131 | -PhpVer $PhpVer ` 132 | -PhpTs ${{ inputs.phpts }} ` 133 | -PhpArch ${{ inputs.phparch }} ` 134 | -PhpVCVer $PhpVCVer 135 | $ret = $lastexitcode 136 | Write-Host "::endgroup::" 137 | } 138 | exit $ret 139 | 140 | - name: Build extension 141 | shell: cmd /c ECHO ::group::Start build extension && %TOOLS_PATH%\env.bat {0} 142 | env: 143 | TOOLS_PATH: ${{ inputs.tools-path }} 144 | UNIX_COLOR: "1" 145 | FIX_PICKLE: ${{ inputs.fix-pickle != '0' && '1' || '0' }} 146 | run: | 147 | powershell ${{github.action_path}}\build.ps1 ^ 148 | -ExtPath ${{ inputs.ext-path }} ^ 149 | -ToolsPath ${{ inputs.tools-path }} ^ 150 | -ExtName tdengine ^ 151 | ${{ inputs.conf-args }} || EXIT /b 1 152 | ECHO ::endgroup:: 153 | 154 | - name: Install extension 155 | shell: cmd /c ECHO ::group::Start install extension && %TOOLS_PATH%\env.bat {0} 156 | env: 157 | TOOLS_PATH: ${{ inputs.tools-path }} 158 | UNIX_COLOR: "1" 159 | run: | 160 | IF 1==${{ inputs.install }} ( powershell ${{github.action_path}}\install.ps1 ^ 161 | -ExtPath ${{ inputs.ext-path }} ^ 162 | -Enable ${{ inputs.enable-extension }} ^ 163 | -ExtName tdengine ) || EXIT /b 1 164 | ECHO ::endgroup:: 165 | -------------------------------------------------------------------------------- /.github/workflows/winext/build.ps1: -------------------------------------------------------------------------------- 1 | #php extension builder 2 | 3 | param ( 4 | [string]$ToolsPath = "C:\tools\phpdev", 5 | [string]$ExtPath = ".", 6 | [string]$ExtName, 7 | [parameter(ValueFromRemainingArguments = $true)] 8 | [string[]]$ExtraArgs 9 | ) 10 | 11 | $scriptPath = Split-Path -parent $MyInvocation.MyCommand.Definition 12 | . "$scriptPath\utils.ps1" -ToolName "build" -MaxTry $MaxTry 13 | 14 | info "Start building php extension" 15 | $origwd = (Get-Location).Path 16 | Set-Location $ExtPath 17 | 18 | info "Phpize it" 19 | phpize.bat 20 | if (0 -Ne $lastexitcode){ 21 | err "Failed phpize it, are we at ext dir?" 22 | Set-Location $origwd 23 | exit 1 24 | } 25 | $buildargs = ,".\configure.bat" 26 | if("".Equals($ExtName)){ 27 | warn "No ext name given, will not prepend --enable-extname arg" 28 | }else{ 29 | $buildargs += ,"--enable-$ExtName" 30 | } 31 | $buildargs += ,"--with-php-build=$ToolsPath\deps" 32 | $buildargs += $ExtraArgs 33 | info "Configure it with arg $buildargs" 34 | Invoke-Expression "$buildargs" 35 | if (0 -Ne $lastexitcode){ 36 | err "Failed configure." 37 | Set-Location $origwd 38 | exit 1 39 | } 40 | 41 | if ("${env:FIX_PICKLE}" -Eq "1"){ 42 | info "Modify config.pickle.h to avoid C4005" 43 | $picklefn = "${env:DEVPACK_PATH}\include\main\config.pickle.h" 44 | $orig = Get-Content -Raw $picklefn 45 | $modified = $orig 46 | 47 | $definitions = @( 48 | "PHP_BUILD_SYSTEM", 49 | "PHP_BUILD_PROVIDER", 50 | "PHP_LINKER_MAJOR", 51 | "PHP_LINKER_MINOR", 52 | "__SSE__", 53 | "__SSE2__", 54 | "__SSE3__", 55 | "__SSSE3__", 56 | "__SSE4_1__", 57 | "__SSE4_2__", 58 | "PHP_SIMD_SCALE" 59 | ) 60 | foreach ($definition in $definitions){ 61 | $re = "^(\s*#\s*define\s*" + $definition + ".+)$" 62 | $modified = $modified -Replace ($re, '// $1') 63 | } 64 | # avoid utf8 bom 65 | [System.IO.File]::WriteAllLines($picklefn, $modified) 66 | } 67 | 68 | info "Start nmake" 69 | nmake 70 | if (0 -Ne $lastexitcode){ 71 | err "Failed nmake." 72 | Set-Location $origwd 73 | exit 1 74 | } 75 | Set-Location $origwd 76 | 77 | exit 0 78 | -------------------------------------------------------------------------------- /.github/workflows/winext/deps.ps1: -------------------------------------------------------------------------------- 1 | # php deps downloader 2 | 3 | param ( 4 | [Object[]]$DllDeps, 5 | [int]$MaxTry = 3, 6 | [string]$ToolsPath = "C:\tools\phpdev", 7 | [string]$PhpBin = "php", 8 | [string]$PhpVer = "", 9 | [string]$PhpVCVer = "", 10 | [string]$PhpArch = "x64", 11 | [bool]$Staging = $false, 12 | [bool]$PhpTs = $false, 13 | [bool]$DryRun = $false 14 | ) 15 | 16 | $scriptPath = Split-Path -parent $MyInvocation.MyCommand.Definition 17 | . "$scriptPath\utils.ps1" -ToolName "deps" -MaxTry $MaxTry 18 | 19 | if($DllDeps.Length -Lt 1){ 20 | warn "No deps specified, just skipped." 21 | exit 0 22 | } 23 | 24 | # things we used later 25 | if ("".Equals($PhpVer)){ 26 | try{ 27 | $PhpVer = & $PhpBin -r "echo PHP_MAJOR_VERSION . '.' . PHP_MINOR_VERSION . PHP_EOL;" 28 | $phpinfo = & $PhpBin -i 29 | $PhpVCVer = ($phpinfo | Select-String -Pattern 'PHP Extension Build => .+,(.+)' -CaseSensitive -List).Matches.Groups[1] 30 | $PhpArch = ($phpinfo | Select-String -Pattern 'Architecture => (.+)' -CaseSensitive -List).Matches.Groups[1] 31 | }finally{ 32 | if( 33 | !$PhpVer -Or 34 | !$PhpVCVer -Or 35 | !$PhpArch 36 | ){ 37 | err "Cannot determine php attributes, do you have php in PATH?" 38 | warn "phpver: $PhpVer" 39 | warn "phpvcver: $PhpVCVer" 40 | warn "phparch: $PhpArch" 41 | exit 1 42 | } 43 | } 44 | } 45 | 46 | $PhpVCVer = $PhpVCVer.ToLower() 47 | 48 | info "Try to fetch deps series list from windows.php.net" 49 | if($Staging){ 50 | $stagingStr = "staging" 51 | }else{ 52 | $stagingStr = "stable" 53 | } 54 | $series = (fetchpage "https://windows.php.net/downloads/php-sdk/deps/series/packages-$PhpVer-$PhpVCVer-$PhpArch-$stagingStr.txt").Content 55 | if(!$series){ 56 | warn "Cannot get series information from windows.php.net, try file list instead" 57 | $filelist = (fetchpage ("https://windows.php.net/downloads/php-sdk/deps/" + $PhpVCVer.ToLower() + "/$PhpArch/")).Content 58 | if(!$filelist){ 59 | err "Neither series file nor file list can be got, aborting" 60 | exit 1 61 | } 62 | } 63 | 64 | $downloadeddeps = [System.Collections.ArrayList]@() 65 | foreach ($depname in $DllDeps) { 66 | $depfile = $null 67 | if($series){ 68 | foreach ($filename in $series.Split()) { 69 | if($filename.StartsWith($depname)){ 70 | info "Found file $filename for dep $depname" 71 | $depfile = $filename 72 | break 73 | } 74 | } 75 | }else{ 76 | $fnver = searchfile $filelist -Pattern ("$depname-:-" + $PhpVCVer.ToLower() + "-$PhpArch.zip") 77 | $depfile = $fnver[0] 78 | } 79 | 80 | if(!$depfile){ 81 | err "Cannot find dep file for $depname" 82 | exit 1 83 | } 84 | $downloadeddeps.Add($depfile) | Out-Null 85 | if($DryRun){ 86 | continue 87 | } 88 | 89 | info "Downloading $filename from windows.php.net" 90 | provedir "$ToolsPath" 91 | provedir "$ToolsPath\deps" 92 | $dest = "$ToolsPath\deps\$depfile" 93 | if(Test-Path $dest -PathType Leaf){ 94 | warn "$depfile is already provided, instant extract it." 95 | }else { 96 | $uri = "https://windows.php.net/downloads/php-sdk/deps/$PhpVCVer/$PhpArch/$depfile" 97 | $ret = dlwithhash -Uri $uri -Dest $dest 98 | if (!$ret){ 99 | err "Failed download $uri." 100 | exit 1 101 | } 102 | } 103 | info "Unzipping $dest to $ToolsPath\deps\" 104 | 105 | Expand-Archive $dest -Destination "$ToolsPath\deps\" -Force 106 | } 107 | 108 | # if we are in github workflows, set downloaded as output 109 | if(${env:CI} -Eq "true"){ 110 | $s = ( $downloadeddeps | Sort-Object ) -Join "_" 111 | Write-Host "::set-output name=downloadeddeps::$s" 112 | } 113 | 114 | info "Done." 115 | 116 | exit 0 117 | -------------------------------------------------------------------------------- /.github/workflows/winext/devpack.ps1: -------------------------------------------------------------------------------- 1 | # php dev-pack downloader 2 | 3 | param ( 4 | [int]$MaxTry = 3, 5 | [string]$ToolsPath = "C:\tools\phpdev", 6 | [string]$PhpBin = "php", 7 | [string]$PhpVer = "", 8 | [string]$PhpVCVer = "", 9 | [string]$PhpArch = "x64", 10 | [bool]$PhpTs = $false, 11 | [bool]$DryRun = $false 12 | ) 13 | 14 | $scriptPath = Split-Path -parent $MyInvocation.MyCommand.Definition 15 | . "$scriptPath\utils.ps1" -ToolName "devpack" -MaxTry $MaxTry 16 | 17 | if ("".Equals($PhpVer)){ 18 | try{ 19 | $PhpVer = & $PhpBin -r "echo PHP_MAJOR_VERSION . '.' . PHP_MINOR_VERSION . PHP_EOL;" 20 | $phpinfo = & $PhpBin -i 21 | $PhpVCVer = ($phpinfo | Select-String -Pattern 'PHP Extension Build => .+,(.+)' -CaseSensitive -List).Matches.Groups[1] 22 | $PhpArch = ($phpinfo | Select-String -Pattern 'Architecture => (.+)' -CaseSensitive -List).Matches.Groups[1] 23 | $phpvar = & $PhpBin -r "echo (PHP_ZTS?'':'n') . 'ts-$PhpVCVer-$PhpArch' . PHP_EOL;" 24 | $dashnts = & $PhpBin -r "echo (PHP_ZTS?'':'-nts') . PHP_EOL;" 25 | $underscorets = & $PhpBin -r "echo (PHP_ZTS?'_TS':'') . PHP_EOL;" 26 | }finally{ 27 | } 28 | }else{ 29 | if($PhpTs){ 30 | $phpvar = "ts-$PhpVCVer-$PhpArch" 31 | $dashnts = "" 32 | $underscorets = "_TS" 33 | }else{ 34 | $phpvar = "nts-$PhpVCVer-$PhpArch" 35 | $dashnts = "-nts" 36 | $underscorets = "" 37 | } 38 | } 39 | if( 40 | !$PhpVer -Or 41 | !$PhpVCVer -Or 42 | !$PhpArch -Or 43 | !$phpvar 44 | ){ 45 | err "Cannot determine php attributes, do you have php in PATH?" 46 | err "You can also specify vals via arguments" 47 | warn "phpver: $PhpVer" 48 | warn "phpvcver: $PhpVCVer" 49 | warn "phparch: $PhpArch" 50 | warn "phpvar: $phpvar" 51 | exit 1 52 | } 53 | 54 | 55 | function fetchdevpack(){ 56 | if ($info.$PhpVer.$phpvar) { 57 | info "Found target version on releases, try using latest devpack." 58 | $latest = $info.$PhpVer.$phpvar."devel_pack" 59 | # if we are in github workflows, set ver as output 60 | if(${env:CI} -Eq "true"){ 61 | Write-Host ('::set-output name=devpackver::' + $info.$PhpVer."version") 62 | } 63 | if($DryRun){ 64 | return 65 | } 66 | 67 | if($latest.sha1){ 68 | $hash = $latest.sha1 69 | $hashmethod = "SHA1" 70 | }elseif($latest.sha256){ 71 | $hash = $latest.sha256 72 | $hashmethod = "SHA256" 73 | }else{ 74 | warn "No hash for this file provided or not supported." 75 | } 76 | $dest = "$ToolsPath\" + ($latest.path) 77 | 78 | if($hashmethod -And (Test-Path $dest -PathType Leaf)){ 79 | if($hashmethod -And $hash -Eq (Get-FileHash $dest -Algorithm $Hashmethod).Hash){ 80 | warn "$dest is already provided, skipping downloading." 81 | return $dest 82 | } 83 | } 84 | 85 | provedir $ToolsPath 86 | $ret = dlwithhash ` 87 | -Uri ("https://windows.php.net/downloads/releases/" + ($latest.path)) ` 88 | -Dest $dest ` 89 | -Hash $hash ` 90 | -Hashmethod $hashmethod 91 | if ($ret){ 92 | return $dest 93 | } 94 | } else { 95 | info "Target version is not active release or failed to download releases info, try search in file list." 96 | try{ 97 | $page = fetchpage "https://windows.php.net/downloads/releases/archives/" 98 | $groups = ($page | Select-String ` 99 | -List ` 100 | -AllMatches ` 101 | -Pattern ('(?:php-devel-pack-' + $PhpVer + '.(?:\d+)' + $dashnts +'-Win32-' + $PhpVCVer + '-' + $PhpArch + '.zip)')).Matches.Groups 102 | $used = 0 103 | foreach ($item in $groups) { 104 | $minor = ($item | Select-String -Pattern ($PhpVer + '.(\d+)')).Matches.Groups[1].ToString() -As "int" 105 | if ($minor -Gt $used){ 106 | $used = $minor 107 | } 108 | } 109 | $fn = 'php-devel-pack-' + $PhpVer + '.' + $used + $dashnts +'-Win32-' + $PhpVCVer + '-' + $PhpArch + '.zip' 110 | # if we are in github workflows, set ver as output 111 | if(${env:CI} -Eq "true"){ 112 | Write-Host "::set-output name=devpackver::$PhpVer.$used" 113 | } 114 | }catch [System.Net.WebException],[System.IO.IOException]{ 115 | warn "Cannot fetch archives list, use oldest instead" 116 | Write-Host $_ 117 | $fn = 'php-devel-pack-' + $PhpVer + '.0' + $dashnts +'-Win32-' + $PhpVCVer + '-' + $PhpArch + '.zip' 118 | # if we are in github workflows, set ver as output 119 | if(${env:CI} -Eq "true"){ 120 | Write-Host "::set-output name=devpackver::$PhpVer.0" 121 | } 122 | } 123 | if($DryRun){ 124 | return 125 | } 126 | #Write-Host $fn 127 | provedir $ToolsPath 128 | $ret = dlwithhash -Uri "https://windows.php.net/downloads/releases/archives/$fn" -Dest "$ToolsPath\$fn" 129 | if ($ret){ 130 | return "$ToolsPath\$fn" 131 | } 132 | } 133 | } 134 | 135 | info "Finding devpack for PHP $PhpVer $phpvar" 136 | $info = fetchjson -Uri "https://windows.php.net/downloads/releases/releases.json" 137 | if(!$info){ 138 | warn "Cannot fetch php releases info from windows.php.net." 139 | } 140 | $zipdest = fetchdevpack 141 | if($DryRun){ 142 | return 143 | } 144 | if (-Not $zipdest){ 145 | err "Failed download devpack zip." 146 | exit 1 147 | } 148 | 149 | info "Done downloading devpack, unzip it." 150 | 151 | try{ 152 | # if possible, should we use -PassThru to get file list? 153 | Expand-Archive $zipdest -Destination $ToolsPath -Force | Out-Host 154 | }catch { 155 | err "Cannot unzip downloaded zip $zipdest to $ToolsPath, that's strange." 156 | Write-Host $_ 157 | exit 1 158 | } 159 | $sa = New-Object -ComObject Shell.Application 160 | $dirname = ($sa.NameSpace($zipdest).Items() | Select-Object -Index 0).Name 161 | 162 | info "Done unzipping devpack, generate env.bat." 163 | 164 | # Since setup-php only provides Release version PHP, yet we only support Release 165 | # Maybe sometimes we can build PHP by ourself? 166 | $content=" 167 | @ECHO OFF 168 | SET BUILD_DIR=$PhpArch\Release$underscorets 169 | SET PATH=$ToolsPath\$dirname;%PATH% 170 | SET DEVPACK_PATH=$ToolsPath\$dirname 171 | $ToolsPath\php-sdk-binary-tools\phpsdk-starter.bat -c $PhpVCVer -a $PhpArch -t %* 172 | " 173 | [IO.File]::WriteAllLines("$ToolsPath\env.bat", $content) 174 | 175 | info "Done preparing dev-pack" 176 | 177 | exit 0 178 | -------------------------------------------------------------------------------- /.github/workflows/winext/devpack_master.ps1: -------------------------------------------------------------------------------- 1 | # php dev-pack downloader from shivammathur/php-builder-windows 2 | 3 | param ( 4 | [int]$MaxTry = 3, 5 | [string]$ToolsPath = "C:\tools\phpdev", 6 | [string]$PhpArch = "x64", 7 | [string]$PhpVCVer = "vs16", 8 | [bool]$PhpTs = $false, 9 | [string]$Release = "master" 10 | ) 11 | 12 | $scriptPath = Split-Path -parent $MyInvocation.MyCommand.Definition 13 | . "$scriptPath\utils.ps1" -ToolName "devpack-master" -MaxTry $MaxTry 14 | 15 | if($PhpTs){ 16 | $nts = "ts" 17 | $underscorets = "_TS" 18 | }else{ 19 | $nts = "nts" 20 | $underscorets = "" 21 | } 22 | 23 | function fetchdevpack(){ 24 | info "Fetching php master dev pack from shivammathur/php-builder-windows" 25 | provedir $ToolsPath 26 | 27 | $VCVer = $PhpVCVer.ToLower() 28 | $fn = "php-debug-pack-master-$nts-Win32-$VCVer-$PhpArch.zip" 29 | $ret = dlwithhash -Uri "https://github.com/shivammathur/php-builder-windows/releases/download/$Release/php-devel-pack-master-$nts-windows-$VCVer-$PhpArch.zip" -Dest "$ToolsPath\$fn" 30 | if ($ret){ 31 | return "$ToolsPath\$fn" 32 | } 33 | } 34 | 35 | info "Finding devpack for PHP master" 36 | $zipdest = fetchdevpack 37 | if (-Not $zipdest){ 38 | err "Failed download devpack zip." 39 | exit 1 40 | } 41 | 42 | info "Done downloading devpack, unzip it." 43 | 44 | try{ 45 | # if possible, should we use -PassThru to get file list? 46 | Expand-Archive $zipdest -Destination $ToolsPath -Force | Out-Host 47 | }catch { 48 | err "Cannot unzip downloaded zip $zipdest to $ToolsPath, that's strange." 49 | Write-Host $_ 50 | exit 1 51 | } 52 | $sa = New-Object -ComObject Shell.Application 53 | $dirname = ($sa.NameSpace($zipdest).Items() | Select-Object -Index 0).Name 54 | 55 | info "Done unzipping devpack, generate env.bat." 56 | 57 | $content=" 58 | @ECHO OFF 59 | SET BUILD_DIR=$PhpArch\Release$underscorets 60 | SET PATH=$ToolsPath\$dirname;%PATH% 61 | SET DEVPACK_PATH=$ToolsPath\$dirname 62 | $ToolsPath\php-sdk-binary-tools\phpsdk-starter.bat -c $PhpVCVer -a $PhpArch -t %* 63 | " 64 | [IO.File]::WriteAllLines("$ToolsPath\env.bat", $content) 65 | 66 | info "Done preparing dev-pack" 67 | 68 | exit 0 69 | -------------------------------------------------------------------------------- /.github/workflows/winext/getphp.ps1: -------------------------------------------------------------------------------- 1 | # php downloader 2 | 3 | param ( 4 | [int]$MaxTry = 3, 5 | [string]$ToolsPath = "C:\tools\phpdev", 6 | [string]$PhpVer = "", 7 | [string]$PhpVCVer = "", 8 | [string]$PhpArch = "x64", 9 | [bool]$PhpTs = $false, 10 | [bool]$DryRun = $false 11 | ) 12 | 13 | $scriptPath = Split-Path -parent $MyInvocation.MyCommand.Definition 14 | . "$scriptPath\utils.ps1" -ToolName "getphp" -MaxTry $MaxTry 15 | 16 | $guessedVCVers = @{ 17 | "8.1" = "VS16"; 18 | "8.0" = "VS16"; 19 | "7.4" = "VC15"; 20 | "7.3" = "VC15"; 21 | "7.2" = "VC15"; 22 | } 23 | 24 | if ("".Equals($PhpVCVer)){ 25 | $PhpVCVer = $guessedVCVers[$PhpVer] 26 | } 27 | 28 | if( 29 | !$PhpVer -Or 30 | !$PhpVCVer -Or 31 | !$PhpArch 32 | ){ 33 | err "Cannot determine php attributes" 34 | warn "phpver: $PhpVer" 35 | warn "phpvcver: $PhpVCVer" 36 | warn "phparch: $phparch" 37 | exit 1 38 | } 39 | 40 | if($PhpTs){ 41 | $phpvar = "ts-$PhpVCVer-$PhpArch" 42 | $dashnts = "" 43 | $underscorets = "_TS" 44 | }else{ 45 | $phpvar = "nts-$PhpVCVer-$PhpArch" 46 | $dashnts = "-nts" 47 | $underscorets = "" 48 | } 49 | 50 | info "Fetching releases list for PHP $PhpVer $phpvar" 51 | $info = fetchjson -Uri "https://windows.php.net/downloads/releases/releases.json" 52 | if(!$info){ 53 | warn "Cannot fetch php releases info from windows.php.net." 54 | } 55 | 56 | $dest = $null 57 | if($info.$PhpVer.$phpvar){ 58 | info "Found in releases, use it" 59 | $latest = $info.$PhpVer.$phpvar."zip" 60 | # if we are in github workflows, set ver as output 61 | if(${env:CI} -Eq "true"){ 62 | Write-Host ('::set-output name=phpver::' + $info.$PhpVer."version") 63 | } 64 | if($DryRun){ 65 | return 66 | } 67 | if($latest.sha1){ 68 | $hash = $latest.sha1 69 | $hashmethod = "SHA1" 70 | }elseif($latest.sha256){ 71 | $hash = $latest.sha256 72 | $hashmethod = "SHA256" 73 | }else{ 74 | warn "No hash for this file provided or not supported." 75 | } 76 | $dest = "$ToolsPath\" + ($latest.path) 77 | 78 | $skip = $false 79 | if($hashmethod -And (Test-Path $dest -PathType Leaf)){ 80 | if($hashmethod -And $hash -Eq (Get-FileHash $dest -Algorithm $Hashmethod).Hash){ 81 | warn "$dest is already provided, skipping downloading." 82 | $skip = $true 83 | } 84 | } 85 | 86 | if (-Not $skip){ 87 | provedir $ToolsPath 88 | $ret = dlwithhash ` 89 | -Uri ("https://windows.php.net/downloads/releases/" + ($latest.path)) ` 90 | -Dest $dest ` 91 | -Hash $hash ` 92 | -Hashmethod $hashmethod 93 | if (!$ret){ 94 | err "Cannot fetch $fn" 95 | exit 1 96 | } 97 | } 98 | }else{ 99 | info "Cannot find in releases, fetching php list from windows.php.net" 100 | $page = fetchpage "https://windows.php.net/downloads/releases/archives/" 101 | $fnver = searchfile $page -Pattern ('php-(?' + $PhpVer + '[^-]+?)' + $dashnts +'-Win32-' + $PhpVCVer + '-' + $PhpArch + '.zip') 102 | if(!$fnver){ 103 | warn "Cannot fetch archives list, use oldest instead" 104 | Write-Host $_ 105 | $fn = 'php-' + $PhpVer + '.0' + $dashnts +'-Win32-' + $PhpVCVer + '-' + $PhpArch + '.zip' 106 | $ver = $PhpVer + '.0' 107 | }else{ 108 | $fn = $fnver[0] 109 | $ver = $fnver[1] 110 | } 111 | # if we are in github workflows, set ver as output 112 | if(${env:CI} -Eq "true"){ 113 | Write-Host "::set-output name=phpver::$ver" 114 | } 115 | if($DryRun){ 116 | return 117 | } 118 | $dest = "$ToolsPath\$fn" 119 | $ret = dlwithhash -Uri "https://windows.php.net/downloads/releases/archives/$fn" -Dest $dest 120 | if (!$ret){ 121 | err "Cannot fetch $fn" 122 | exit 1 123 | } 124 | } 125 | 126 | if (!$dest){ 127 | exit 1 128 | } 129 | 130 | info "Done downloading php as $dest, unzip it." 131 | try{ 132 | # if possible, should we use -PassThru to get file list? 133 | Expand-Archive $dest -Destination $ToolsPath\php -Force | Out-Host 134 | }catch { 135 | err "Cannot unzip downloaded zip $dest to $ToolsPath\php, that's strange." 136 | Write-Host $_ 137 | exit 1 138 | } 139 | 140 | info "Done unzipping php, generate phpenv.bat." 141 | 142 | $env:Path = "$ToolsPath\php;$env:Path" 143 | $env:BUILD_DIR = "$PhpArch\Release$underscorets" 144 | $content="@ECHO OFF 145 | SET PATH=$ToolsPath\php;%PATH% 146 | %* 147 | " 148 | [IO.File]::WriteAllLines("$ToolsPath\phpenv.bat", $content) 149 | 150 | info "Done getphp" 151 | 152 | exit 0 153 | -------------------------------------------------------------------------------- /.github/workflows/winext/install.ps1: -------------------------------------------------------------------------------- 1 | # php extension installer 2 | 3 | param ( 4 | [string]$ExtName, 5 | [string]$PhpBin= "php", 6 | [string]$ExtPath = ".", 7 | [bool]$Enable = $false 8 | ) 9 | 10 | $scriptPath = Split-Path -parent $MyInvocation.MyCommand.Definition 11 | . "$scriptPath\utils.ps1" -ToolName "install" -MaxTry $MaxTry 12 | 13 | info "Start installing php extension" 14 | $origwd = (Get-Location).Path 15 | Set-Location $ExtPath 16 | 17 | $phppath = ((Get-Command $PhpBin).Source | Select-String -Pattern '(.+)\\php\.exe').Matches.Groups[1].Value 18 | $extdir = & $PhpBin -r "echo ini_get('extension_dir');" 19 | $extdir_ini = "" 20 | if (![System.IO.Path]::IsPathRooted($extdir)){ 21 | # if it's not absolute, it's relative path to $phppath 22 | # we need set full path in ini 23 | $extdir = "$phppath\$extdir" 24 | $extdir_ini = "extension_dir=$extdir" 25 | } 26 | $inipath = "$phppath\php.ini" 27 | 28 | if(-Not (Test-Path "$env:BUILD_DIR\php_$ExtName.dll" -PathType Leaf)){ 29 | err "Could not found $env:BUILD_DIR\php_$ExtName.dll, do we running in env.bat?" 30 | Set-Location $origwd 31 | exit 1 32 | } 33 | 34 | if(-Not (Test-Path $extdir -PathType Container)){ 35 | info "Create extension dir $extdir" 36 | New-Item -Path $extdir -ItemType Container | Out-Null 37 | } 38 | 39 | info "Copy $env:BUILD_DIR\php_$ExtName.dll to $extdir" 40 | Copy-Item "$env:BUILD_DIR\php_$ExtName.dll" $extdir | Out-Null 41 | 42 | $ext_ini = "" 43 | if($Enable){ 44 | $ext_ini = "extension=$ExtName" 45 | } 46 | 47 | try{ 48 | $ini = Get-Content $inipath 49 | }catch{ 50 | $ini = "" 51 | } 52 | 53 | $match = $ini | Select-String -Pattern ('^\s*extension\s*=\s*["' + "'" + "]*$ExtName['" + '"' + ']*\s*') 54 | if($match.Matches){ 55 | warn ("Ini entry extension=$ExtName is already setted at $inipath line" + $match.LineNumber + ", skipping ini modification") 56 | }elseif($Enable -Or ($extdir_ini -Ne "")){ 57 | info ("Append `"$extdir_ini`", `"$ext_ini`" to " + $inipath) 58 | $content = " 59 | $extdir_ini 60 | $ext_ini 61 | " 62 | $content | Out-File -Encoding utf8 -Append $inipath 63 | } 64 | 65 | $define = "" 66 | if (!$Enable){ 67 | $define = "-dextension=tdengine" 68 | } 69 | 70 | info "Run 'php $define --ri $ExtName'" 71 | & $PhpBin $define --ri $ExtName 72 | if(0 -Ne $lastexitcode){ 73 | exit 1 74 | } 75 | 76 | Set-Location $origwd 77 | 78 | exit 0 -------------------------------------------------------------------------------- /.github/workflows/winext/release.ps1: -------------------------------------------------------------------------------- 1 | # create a gh release 2 | 3 | param ( 4 | [string]$Token, 5 | [string]$Repo, 6 | [string]$TagName, 7 | [string]$body, 8 | [bool]$prerelease=$false, 9 | [bool]$draft=$true 10 | ) 11 | 12 | $scriptPath = Split-Path -parent $MyInvocation.MyCommand.Definition 13 | . "$scriptPath\utils.ps1" -ToolName "release" -MaxTry 1 14 | 15 | if(-Not $Repo -Or -Not $TagName -Or -Not $Token){ 16 | err "Needs repo, release tagname and gh token to work." 17 | exit 1 18 | } 19 | 20 | # gh api headers 21 | $headers = @{ 22 | "accept"="application/vnd.github.v3+json"; 23 | "content-type"="application/json"; 24 | "authorization"="Bearer ${Token}"; 25 | } 26 | try { 27 | $ret = fetchjson ` 28 | -Uri "https://api.github.com/repos/$Repo/releases/tags/$TagName" ` 29 | -Headers $headers 30 | } catch { 31 | } 32 | if(!$ret){ 33 | $data = @{ 34 | "tag_name"=$TagName; 35 | "name"=$TagName; 36 | "body"=$body; 37 | "draft"=$true; 38 | "prerelease"=$true; 39 | } 40 | $ret = fetchjson ` 41 | -Body ($data | ConvertTo-Json -Compress) ` 42 | -Method "POST" ` 43 | -Uri "https://api.github.com/repos/$Repo/releases" ` 44 | -Headers $headers 45 | if(!$ret){ 46 | err "Failed create release" 47 | exit 1 48 | } 49 | } 50 | 51 | Write-Host ("::set-output name=upload_url::" + $ret."upload_url") 52 | Write-Host ("::set-output name=id::" + $ret."id") 53 | -------------------------------------------------------------------------------- /.github/workflows/winext/uploader.ps1: -------------------------------------------------------------------------------- 1 | # dll release uploader (via artifact) 2 | 3 | param ( 4 | [int]$MaxTry=3, 5 | [string]$Repo, 6 | [string]$RelID, 7 | [string]$Token 8 | ) 9 | 10 | $scriptPath = Split-Path -parent $MyInvocation.MyCommand.Definition 11 | . "$scriptPath\utils.ps1" -ToolName "uploader" -MaxTry $MaxTry 12 | 13 | # gh api headers 14 | $headers = @{ 15 | "accept"="application/vnd.github.v3+json"; 16 | "content-type"="application/json"; 17 | "authorization"="Bearer ${Token}"; 18 | } 19 | 20 | if(-Not $Repo -Or -Not $Token -Or -Not $RelID){ 21 | err "Needs repo name and gh token to work." 22 | #exit 1 23 | } 24 | 25 | $RelInfo = fetchjson ` 26 | -Uri "https://api.github.com/repos/$Repo/releases/$RelID" ` 27 | -Headers $headers 28 | if(!$RelInfo){ 29 | err "Failed fetch release information" 30 | return 31 | } 32 | 33 | $match = $RelInfo."upload_url" | Select-String -Pattern "(?(?:http|https)://.+)(?\{.+\})" 34 | $uploadUrl = ($match.Matches[0].Groups["url"]).ToString() + "?name=" 35 | 36 | $RunID = $null 37 | $jobdata = $null 38 | 39 | $note = "`n## Hashes and notes`n`n" + ` 40 | "| File name | Size (in bytes) | SHA256 sum | Build log | Tests result |`n" + ` 41 | "| - | - | - | - | - |`n" 42 | 43 | # read all jsons for all dlls 44 | Get-ChildItem . | Sort-Object -Property Name | ForEach-Object -Process { 45 | if($_.Name.EndsWith(".dll")){ 46 | $fn = $_.Name 47 | $jsonfn = "${fn}.json" 48 | if (Test-Path $jsonfn -Type Leaf){ 49 | info "Read information from $jsonfn" 50 | $data = Get-Content $jsonfn | ConvertFrom-Json 51 | if($fn -Ne $data.name){ 52 | warn "Not same filename, bad json, skip it" 53 | continue 54 | } 55 | if(-Not $RunID){ 56 | $RunID = $data.runid 57 | $jobdata = (fetchjson ` 58 | -Headers $headers ` 59 | -Uri "https://api.github.com/repos/$Repo/actions/runs/$RunID/jobs")."jobs" 60 | }else{ 61 | if ($RunID -Ne $data.runid){ 62 | warn "Not same runid, bad json, skip it" 63 | continue 64 | } 65 | } 66 | if((Get-FileHash -Algorithm SHA256 $fn).Hash -Ne $data.hash){ 67 | warn "Bad dll hash, skip it" 68 | continue 69 | } 70 | $link = $null 71 | foreach($job in $jobdata) { 72 | if($job.name.ToString().Contains($data.jobname)){ 73 | $link = $job."html_url" 74 | info "Workflow run link is $link" 75 | } 76 | } 77 | $linkstr = "[link](${link})" 78 | if(-Not $link){ 79 | warn "Not found work run, strange" 80 | $linkstr = "-" 81 | } 82 | info "Uploading file $fn" 83 | $ret = Invoke-WebRequest ` 84 | -Uri "$uploadUrl$fn" ` 85 | -Method "POST" ` 86 | -ContentType "application/zip" ` 87 | -Headers $headers ` 88 | -InFile $fn 89 | if(-Not $ret){ 90 | warn "Failed to upload $fn" 91 | continue 92 | } 93 | $size = $data.size 94 | $hash = $data.hash 95 | $result = $data.result 96 | $note += "| ${fn} | ${size} | ${hash} | ${linkstr} | ${result} |`n" 97 | } 98 | } 99 | } 100 | 101 | info "Fetching original notes" 102 | $note = $RelInfo.body.ToString() + $note 103 | $patch = @{ 104 | "body"="$note"; 105 | } | ConvertTo-Json -Compress 106 | 107 | info "Repost note" 108 | $ret = fetchjson ` 109 | -Body $patch ` 110 | -Method "PATCH" ` 111 | -Uri "https://api.github.com/repos/$Repo/releases/$RelID" ` 112 | -Headers $headers 113 | if (-Not $ret){ 114 | err "Failed patch notes" 115 | exit 1 116 | } 117 | 118 | info Done 119 | 120 | -------------------------------------------------------------------------------- /.github/workflows/winext/utils.ps1: -------------------------------------------------------------------------------- 1 | 2 | param ( 3 | [string]$ToolName, 4 | [int]$MaxTry 5 | ) 6 | function err{ 7 | param ( $Msg ) 8 | script:log -Msg $Msg -Tag "ERR" -ColorName "Red" -ColorCode "31" 9 | } 10 | function warn{ 11 | param ( $Msg ) 12 | script:log -Msg $Msg -Tag "WRN" -ColorName "Yellow" -ColorCode "33" 13 | } 14 | function info{ 15 | param ( $Msg ) 16 | script:log -Msg $Msg -Tag "IFO" -ColorName "Green" -ColorCode "32" 17 | } 18 | function script:log{ 19 | param ( $Msg, $Tag, $ColorName, $ColorCode ) 20 | if($env:UNIX_COLOR) { 21 | Write-Host -NoNewline (0x1b -As [char]) 22 | Write-Host -NoNewline "[$ColorCode;1m" 23 | } 24 | Write-Host -NoNewline -ForegroundColor $ColorName "[${ToolName}:${Tag}] " 25 | if($env:UNIX_COLOR) { 26 | Write-Host -NoNewline (0x1b -As [char]) 27 | Write-Host -NoNewline "[0;1m" 28 | } 29 | Write-Host -NoNewline -ForegroundColor White "$Msg" 30 | if($env:UNIX_COLOR) { 31 | Write-Host -NoNewline (0x1b -As [char]) 32 | Write-Host -NoNewline "[0m" 33 | } 34 | Write-Host "" 35 | } 36 | 37 | function provedir { 38 | param ($Path) 39 | if (-Not (Test-Path -Path $Path -PathType Container)){ 40 | info "Creating dir" $Path 41 | New-Item -Path $Path -ItemType Container | Out-Null 42 | } 43 | } 44 | 45 | function fetchpage { 46 | param ($Uri, $Headers=$null, $Method="GET", $Body=$null) 47 | for ($i=0; $i -lt $MaxTry; $i++){ 48 | try{ 49 | $ret = Invoke-WebRequest -Uri $Uri -UseBasicParsing -Headers $Headers -Method $Method -Body $Body 50 | return $ret 51 | }catch [System.Net.WebException],[System.IO.IOException]{ 52 | warn "Failed to fetch page ${Uri}, try again." 53 | Write-Host $_ 54 | continue 55 | } 56 | } 57 | return $null 58 | } 59 | 60 | function fetchjson { 61 | param ($Uri, $Headers, $Method="GET", $Body) 62 | $page = fetchpage -Uri $Uri -Headers $Headers -Method $Method -Body $Body 63 | if($page){ 64 | try{ 65 | return ($page | ConvertFrom-Json) 66 | }catch{ 67 | warn "Failed parse page ${Uri} as json." 68 | Write-Host $_ 69 | } 70 | } 71 | return $null 72 | } 73 | 74 | function dlwithhash{ 75 | param ( 76 | $Uri, $Dest, $Hash, $Hashmethod 77 | ) 78 | 79 | for ($i=0; $i -lt $MaxTry; $i++){ 80 | try{ 81 | info "Try to download ${uri}" 82 | Invoke-WebRequest -Uri $Uri -OutFile $Dest -UseBasicParsing | Out-Null 83 | }catch [System.Net.WebException],[System.IO.IOException]{ 84 | warn "Failed download ${uri}." 85 | Write-Host $_ 86 | continue 87 | } 88 | 89 | if($Hashmethod -And -Not $Hash -Eq (Get-FileHash $Dest -Algorithm $Hashmethod).Hash ){ 90 | warn "Bad checksum, remove file $Dest." 91 | Remove-Item $Dest | Out-Null 92 | continue 93 | } 94 | break 95 | } 96 | if ($hashmethod -And -Not $hash -Eq (Get-FileHash $dest -Algorithm $Hashmethod).Hash ){ 97 | warn "Cannot download ${uri}: bad checksum." 98 | return $false 99 | } 100 | return $true 101 | } 102 | 103 | function script:vercompare{ 104 | param ($a,$b,$size) 105 | 106 | for($i = 0; $i -Lt $size; $i++){ 107 | $intvera = $a[$i].ToString() -As [int] 108 | $intverb = $b[$i].ToString() -As [int] 109 | #Write-Host "$intvera $intverb" 110 | if($null -Ne $intvera -And $null -Ne $intverb){ 111 | if($intvera -Eq $intverb){ 112 | continue 113 | } 114 | return $intvera -Gt $intverb 115 | }else{ 116 | if($a[$i].ToString().Equals($b[$i].ToString())){ 117 | continue 118 | } 119 | return $a[$i].ToString() -Gt $b[$i].ToString() 120 | } 121 | } 122 | 123 | return $false 124 | } 125 | function script:filecompare{ 126 | param ($a, $b) 127 | $tuplea = ($a | Select-String -Pattern "^([^.]+)\.([^.]+)\.([^.-]+)$").Matches.Groups 128 | if(!$tuplea.Success){ 129 | $tuplea = ($a | Select-String -Pattern "^([^.]+)\.([^.-]+)$").Matches.Groups 130 | if(!$tuplea.Success){ 131 | $plaina = $a 132 | } 133 | } 134 | $tupleb = ($b | Select-String -Pattern "^([^.]+)\.([^.]+)\.([^.-]+)$").Matches.Groups 135 | if(!$tupleb.Success){ 136 | $tupleb = ($b | Select-String -Pattern "^([^.]+)\.([^.-]+)$").Matches.Groups 137 | if(!$tupleb.Success){ 138 | $plainb = $b 139 | } 140 | } 141 | 142 | if ($plaina -Or $plainb){ 143 | #Write-Host "plain" 144 | if($plaina -Eq $plainb){ 145 | # TODO: use latest (not possible come here yet) 146 | return $false 147 | } 148 | return $plaina -Gt $plainb 149 | } 150 | 151 | $triplea = ,$tuplea[1], $tuplea[2], (&{if($tuplea[3]) {$tuplea[3]} else {"0"}}); 152 | $tripleb = ,$tupleb[1], $tupleb[2], (&{if($tupleb[3]) {$tupleb[3]} else {"0"}}); 153 | #Write-Host $triplea 154 | #Write-Host $tripleb 155 | return (script:vercompare -a $triplea -b $tripleb -size 3) 156 | } 157 | 158 | function searchfile{ 159 | param ($List, $Pattern) 160 | #"3/22/2011 1:30 PM 19378176" 161 | $fileinfore = "(?\d+)/(?\d+)/(?\d+)\s+(?\d+):(?\d+)\s+(?PM|AM)\s+(?\d+)\s+" 162 | 163 | $match = ($List | Select-String ` 164 | -List ` 165 | -Pattern ($fileinfore + '(?' + $Pattern + ')') ).Matches[0] 166 | if(!$match){ 167 | return $null 168 | } 169 | $used = $match.Groups 170 | 171 | for($match; 172 | $match.Success; 173 | $match = $match.NextMatch()){ 174 | if(script:filecompare -a $match.Groups["ver"].ToString() -b $used['ver'].ToString()){ 175 | $used = $match.Groups 176 | } 177 | } 178 | 179 | return @($used['fn'].ToString(), $used['ver'].ToString()) 180 | } -------------------------------------------------------------------------------- /.gitignore: -------------------------------------------------------------------------------- 1 | *.lo 2 | *.la 3 | .libs 4 | acinclude.m4 5 | aclocal.m4 6 | autom4te.cache 7 | build 8 | config.guess 9 | config.h 10 | config.h.in 11 | config.log 12 | config.nice 13 | config.status 14 | config.sub 15 | configure 16 | configure.ac 17 | configure.in 18 | install-sh 19 | libtool 20 | ltmain.sh 21 | Makefile 22 | Makefile.fragments 23 | Makefile.global 24 | Makefile.objects 25 | missing 26 | mkinstalldirs 27 | modules 28 | php_test_results_*.txt 29 | phpt.* 30 | run-test-info.php 31 | run-tests.php 32 | tests/**/*.diff 33 | tests/**/*.out 34 | tests/**/*.php 35 | tests/**/*.exp 36 | tests/**/*.log 37 | tests/**/*.sh 38 | tests/**/*.db 39 | tests/**/*.mem 40 | tmp-php.ini 41 | /.vscode 42 | *.log 43 | /config.h.in~ 44 | -------------------------------------------------------------------------------- /LICENSE: -------------------------------------------------------------------------------- 1 | GNU AFFERO GENERAL PUBLIC LICENSE 2 | Version 3, 19 November 2007 3 | 4 | Copyright (C) 2007 Free Software Foundation, Inc. 5 | Everyone is permitted to copy and distribute verbatim copies 6 | of this license document, but changing it is not allowed. 7 | 8 | Preamble 9 | 10 | The GNU Affero General Public License is a free, copyleft license for 11 | software and other kinds of works, specifically designed to ensure 12 | cooperation with the community in the case of network server software. 13 | 14 | The licenses for most software and other practical works are designed 15 | to take away your freedom to share and change the works. By contrast, 16 | our General Public Licenses are intended to guarantee your freedom to 17 | share and change all versions of a program--to make sure it remains free 18 | software for all its users. 19 | 20 | When we speak of free software, we are referring to freedom, not 21 | price. Our General Public Licenses are designed to make sure that you 22 | have the freedom to distribute copies of free software (and charge for 23 | them if you wish), that you receive source code or can get it if you 24 | want it, that you can change the software or use pieces of it in new 25 | free programs, and that you know you can do these things. 26 | 27 | Developers that use our General Public Licenses protect your rights 28 | with two steps: (1) assert copyright on the software, and (2) offer 29 | you this License which gives you legal permission to copy, distribute 30 | and/or modify the software. 31 | 32 | A secondary benefit of defending all users' freedom is that 33 | improvements made in alternate versions of the program, if they 34 | receive widespread use, become available for other developers to 35 | incorporate. Many developers of free software are heartened and 36 | encouraged by the resulting cooperation. However, in the case of 37 | software used on network servers, this result may fail to come about. 38 | The GNU General Public License permits making a modified version and 39 | letting the public access it on a server without ever releasing its 40 | source code to the public. 41 | 42 | The GNU Affero General Public License is designed specifically to 43 | ensure that, in such cases, the modified source code becomes available 44 | to the community. It requires the operator of a network server to 45 | provide the source code of the modified version running there to the 46 | users of that server. Therefore, public use of a modified version, on 47 | a publicly accessible server, gives the public access to the source 48 | code of the modified version. 49 | 50 | An older license, called the Affero General Public License and 51 | published by Affero, was designed to accomplish similar goals. This is 52 | a different license, not a version of the Affero GPL, but Affero has 53 | released a new version of the Affero GPL which permits relicensing under 54 | this license. 55 | 56 | The precise terms and conditions for copying, distribution and 57 | modification follow. 58 | 59 | TERMS AND CONDITIONS 60 | 61 | 0. Definitions. 62 | 63 | "This License" refers to version 3 of the GNU Affero General Public License. 64 | 65 | "Copyright" also means copyright-like laws that apply to other kinds of 66 | works, such as semiconductor masks. 67 | 68 | "The Program" refers to any copyrightable work licensed under this 69 | License. Each licensee is addressed as "you". "Licensees" and 70 | "recipients" may be individuals or organizations. 71 | 72 | To "modify" a work means to copy from or adapt all or part of the work 73 | in a fashion requiring copyright permission, other than the making of an 74 | exact copy. The resulting work is called a "modified version" of the 75 | earlier work or a work "based on" the earlier work. 76 | 77 | A "covered work" means either the unmodified Program or a work based 78 | on the Program. 79 | 80 | To "propagate" a work means to do anything with it that, without 81 | permission, would make you directly or secondarily liable for 82 | infringement under applicable copyright law, except executing it on a 83 | computer or modifying a private copy. Propagation includes copying, 84 | distribution (with or without modification), making available to the 85 | public, and in some countries other activities as well. 86 | 87 | To "convey" a work means any kind of propagation that enables other 88 | parties to make or receive copies. Mere interaction with a user through 89 | a computer network, with no transfer of a copy, is not conveying. 90 | 91 | An interactive user interface displays "Appropriate Legal Notices" 92 | to the extent that it includes a convenient and prominently visible 93 | feature that (1) displays an appropriate copyright notice, and (2) 94 | tells the user that there is no warranty for the work (except to the 95 | extent that warranties are provided), that licensees may convey the 96 | work under this License, and how to view a copy of this License. If 97 | the interface presents a list of user commands or options, such as a 98 | menu, a prominent item in the list meets this criterion. 99 | 100 | 1. Source Code. 101 | 102 | The "source code" for a work means the preferred form of the work 103 | for making modifications to it. "Object code" means any non-source 104 | form of a work. 105 | 106 | A "Standard Interface" means an interface that either is an official 107 | standard defined by a recognized standards body, or, in the case of 108 | interfaces specified for a particular programming language, one that 109 | is widely used among developers working in that language. 110 | 111 | The "System Libraries" of an executable work include anything, other 112 | than the work as a whole, that (a) is included in the normal form of 113 | packaging a Major Component, but which is not part of that Major 114 | Component, and (b) serves only to enable use of the work with that 115 | Major Component, or to implement a Standard Interface for which an 116 | implementation is available to the public in source code form. A 117 | "Major Component", in this context, means a major essential component 118 | (kernel, window system, and so on) of the specific operating system 119 | (if any) on which the executable work runs, or a compiler used to 120 | produce the work, or an object code interpreter used to run it. 121 | 122 | The "Corresponding Source" for a work in object code form means all 123 | the source code needed to generate, install, and (for an executable 124 | work) run the object code and to modify the work, including scripts to 125 | control those activities. However, it does not include the work's 126 | System Libraries, or general-purpose tools or generally available free 127 | programs which are used unmodified in performing those activities but 128 | which are not part of the work. For example, Corresponding Source 129 | includes interface definition files associated with source files for 130 | the work, and the source code for shared libraries and dynamically 131 | linked subprograms that the work is specifically designed to require, 132 | such as by intimate data communication or control flow between those 133 | subprograms and other parts of the work. 134 | 135 | The Corresponding Source need not include anything that users 136 | can regenerate automatically from other parts of the Corresponding 137 | Source. 138 | 139 | The Corresponding Source for a work in source code form is that 140 | same work. 141 | 142 | 2. Basic Permissions. 143 | 144 | All rights granted under this License are granted for the term of 145 | copyright on the Program, and are irrevocable provided the stated 146 | conditions are met. This License explicitly affirms your unlimited 147 | permission to run the unmodified Program. The output from running a 148 | covered work is covered by this License only if the output, given its 149 | content, constitutes a covered work. This License acknowledges your 150 | rights of fair use or other equivalent, as provided by copyright law. 151 | 152 | You may make, run and propagate covered works that you do not 153 | convey, without conditions so long as your license otherwise remains 154 | in force. You may convey covered works to others for the sole purpose 155 | of having them make modifications exclusively for you, or provide you 156 | with facilities for running those works, provided that you comply with 157 | the terms of this License in conveying all material for which you do 158 | not control copyright. Those thus making or running the covered works 159 | for you must do so exclusively on your behalf, under your direction 160 | and control, on terms that prohibit them from making any copies of 161 | your copyrighted material outside their relationship with you. 162 | 163 | Conveying under any other circumstances is permitted solely under 164 | the conditions stated below. Sublicensing is not allowed; section 10 165 | makes it unnecessary. 166 | 167 | 3. Protecting Users' Legal Rights From Anti-Circumvention Law. 168 | 169 | No covered work shall be deemed part of an effective technological 170 | measure under any applicable law fulfilling obligations under article 171 | 11 of the WIPO copyright treaty adopted on 20 December 1996, or 172 | similar laws prohibiting or restricting circumvention of such 173 | measures. 174 | 175 | When you convey a covered work, you waive any legal power to forbid 176 | circumvention of technological measures to the extent such circumvention 177 | is effected by exercising rights under this License with respect to 178 | the covered work, and you disclaim any intention to limit operation or 179 | modification of the work as a means of enforcing, against the work's 180 | users, your or third parties' legal rights to forbid circumvention of 181 | technological measures. 182 | 183 | 4. Conveying Verbatim Copies. 184 | 185 | You may convey verbatim copies of the Program's source code as you 186 | receive it, in any medium, provided that you conspicuously and 187 | appropriately publish on each copy an appropriate copyright notice; 188 | keep intact all notices stating that this License and any 189 | non-permissive terms added in accord with section 7 apply to the code; 190 | keep intact all notices of the absence of any warranty; and give all 191 | recipients a copy of this License along with the Program. 192 | 193 | You may charge any price or no price for each copy that you convey, 194 | and you may offer support or warranty protection for a fee. 195 | 196 | 5. Conveying Modified Source Versions. 197 | 198 | You may convey a work based on the Program, or the modifications to 199 | produce it from the Program, in the form of source code under the 200 | terms of section 4, provided that you also meet all of these conditions: 201 | 202 | a) The work must carry prominent notices stating that you modified 203 | it, and giving a relevant date. 204 | 205 | b) The work must carry prominent notices stating that it is 206 | released under this License and any conditions added under section 207 | 7. This requirement modifies the requirement in section 4 to 208 | "keep intact all notices". 209 | 210 | c) You must license the entire work, as a whole, under this 211 | License to anyone who comes into possession of a copy. This 212 | License will therefore apply, along with any applicable section 7 213 | additional terms, to the whole of the work, and all its parts, 214 | regardless of how they are packaged. This License gives no 215 | permission to license the work in any other way, but it does not 216 | invalidate such permission if you have separately received it. 217 | 218 | d) If the work has interactive user interfaces, each must display 219 | Appropriate Legal Notices; however, if the Program has interactive 220 | interfaces that do not display Appropriate Legal Notices, your 221 | work need not make them do so. 222 | 223 | A compilation of a covered work with other separate and independent 224 | works, which are not by their nature extensions of the covered work, 225 | and which are not combined with it such as to form a larger program, 226 | in or on a volume of a storage or distribution medium, is called an 227 | "aggregate" if the compilation and its resulting copyright are not 228 | used to limit the access or legal rights of the compilation's users 229 | beyond what the individual works permit. Inclusion of a covered work 230 | in an aggregate does not cause this License to apply to the other 231 | parts of the aggregate. 232 | 233 | 6. Conveying Non-Source Forms. 234 | 235 | You may convey a covered work in object code form under the terms 236 | of sections 4 and 5, provided that you also convey the 237 | machine-readable Corresponding Source under the terms of this License, 238 | in one of these ways: 239 | 240 | a) Convey the object code in, or embodied in, a physical product 241 | (including a physical distribution medium), accompanied by the 242 | Corresponding Source fixed on a durable physical medium 243 | customarily used for software interchange. 244 | 245 | b) Convey the object code in, or embodied in, a physical product 246 | (including a physical distribution medium), accompanied by a 247 | written offer, valid for at least three years and valid for as 248 | long as you offer spare parts or customer support for that product 249 | model, to give anyone who possesses the object code either (1) a 250 | copy of the Corresponding Source for all the software in the 251 | product that is covered by this License, on a durable physical 252 | medium customarily used for software interchange, for a price no 253 | more than your reasonable cost of physically performing this 254 | conveying of source, or (2) access to copy the 255 | Corresponding Source from a network server at no charge. 256 | 257 | c) Convey individual copies of the object code with a copy of the 258 | written offer to provide the Corresponding Source. This 259 | alternative is allowed only occasionally and noncommercially, and 260 | only if you received the object code with such an offer, in accord 261 | with subsection 6b. 262 | 263 | d) Convey the object code by offering access from a designated 264 | place (gratis or for a charge), and offer equivalent access to the 265 | Corresponding Source in the same way through the same place at no 266 | further charge. You need not require recipients to copy the 267 | Corresponding Source along with the object code. If the place to 268 | copy the object code is a network server, the Corresponding Source 269 | may be on a different server (operated by you or a third party) 270 | that supports equivalent copying facilities, provided you maintain 271 | clear directions next to the object code saying where to find the 272 | Corresponding Source. Regardless of what server hosts the 273 | Corresponding Source, you remain obligated to ensure that it is 274 | available for as long as needed to satisfy these requirements. 275 | 276 | e) Convey the object code using peer-to-peer transmission, provided 277 | you inform other peers where the object code and Corresponding 278 | Source of the work are being offered to the general public at no 279 | charge under subsection 6d. 280 | 281 | A separable portion of the object code, whose source code is excluded 282 | from the Corresponding Source as a System Library, need not be 283 | included in conveying the object code work. 284 | 285 | A "User Product" is either (1) a "consumer product", which means any 286 | tangible personal property which is normally used for personal, family, 287 | or household purposes, or (2) anything designed or sold for incorporation 288 | into a dwelling. In determining whether a product is a consumer product, 289 | doubtful cases shall be resolved in favor of coverage. For a particular 290 | product received by a particular user, "normally used" refers to a 291 | typical or common use of that class of product, regardless of the status 292 | of the particular user or of the way in which the particular user 293 | actually uses, or expects or is expected to use, the product. A product 294 | is a consumer product regardless of whether the product has substantial 295 | commercial, industrial or non-consumer uses, unless such uses represent 296 | the only significant mode of use of the product. 297 | 298 | "Installation Information" for a User Product means any methods, 299 | procedures, authorization keys, or other information required to install 300 | and execute modified versions of a covered work in that User Product from 301 | a modified version of its Corresponding Source. The information must 302 | suffice to ensure that the continued functioning of the modified object 303 | code is in no case prevented or interfered with solely because 304 | modification has been made. 305 | 306 | If you convey an object code work under this section in, or with, or 307 | specifically for use in, a User Product, and the conveying occurs as 308 | part of a transaction in which the right of possession and use of the 309 | User Product is transferred to the recipient in perpetuity or for a 310 | fixed term (regardless of how the transaction is characterized), the 311 | Corresponding Source conveyed under this section must be accompanied 312 | by the Installation Information. But this requirement does not apply 313 | if neither you nor any third party retains the ability to install 314 | modified object code on the User Product (for example, the work has 315 | been installed in ROM). 316 | 317 | The requirement to provide Installation Information does not include a 318 | requirement to continue to provide support service, warranty, or updates 319 | for a work that has been modified or installed by the recipient, or for 320 | the User Product in which it has been modified or installed. Access to a 321 | network may be denied when the modification itself materially and 322 | adversely affects the operation of the network or violates the rules and 323 | protocols for communication across the network. 324 | 325 | Corresponding Source conveyed, and Installation Information provided, 326 | in accord with this section must be in a format that is publicly 327 | documented (and with an implementation available to the public in 328 | source code form), and must require no special password or key for 329 | unpacking, reading or copying. 330 | 331 | 7. Additional Terms. 332 | 333 | "Additional permissions" are terms that supplement the terms of this 334 | License by making exceptions from one or more of its conditions. 335 | Additional permissions that are applicable to the entire Program shall 336 | be treated as though they were included in this License, to the extent 337 | that they are valid under applicable law. If additional permissions 338 | apply only to part of the Program, that part may be used separately 339 | under those permissions, but the entire Program remains governed by 340 | this License without regard to the additional permissions. 341 | 342 | When you convey a copy of a covered work, you may at your option 343 | remove any additional permissions from that copy, or from any part of 344 | it. (Additional permissions may be written to require their own 345 | removal in certain cases when you modify the work.) You may place 346 | additional permissions on material, added by you to a covered work, 347 | for which you have or can give appropriate copyright permission. 348 | 349 | Notwithstanding any other provision of this License, for material you 350 | add to a covered work, you may (if authorized by the copyright holders of 351 | that material) supplement the terms of this License with terms: 352 | 353 | a) Disclaiming warranty or limiting liability differently from the 354 | terms of sections 15 and 16 of this License; or 355 | 356 | b) Requiring preservation of specified reasonable legal notices or 357 | author attributions in that material or in the Appropriate Legal 358 | Notices displayed by works containing it; or 359 | 360 | c) Prohibiting misrepresentation of the origin of that material, or 361 | requiring that modified versions of such material be marked in 362 | reasonable ways as different from the original version; or 363 | 364 | d) Limiting the use for publicity purposes of names of licensors or 365 | authors of the material; or 366 | 367 | e) Declining to grant rights under trademark law for use of some 368 | trade names, trademarks, or service marks; or 369 | 370 | f) Requiring indemnification of licensors and authors of that 371 | material by anyone who conveys the material (or modified versions of 372 | it) with contractual assumptions of liability to the recipient, for 373 | any liability that these contractual assumptions directly impose on 374 | those licensors and authors. 375 | 376 | All other non-permissive additional terms are considered "further 377 | restrictions" within the meaning of section 10. If the Program as you 378 | received it, or any part of it, contains a notice stating that it is 379 | governed by this License along with a term that is a further 380 | restriction, you may remove that term. If a license document contains 381 | a further restriction but permits relicensing or conveying under this 382 | License, you may add to a covered work material governed by the terms 383 | of that license document, provided that the further restriction does 384 | not survive such relicensing or conveying. 385 | 386 | If you add terms to a covered work in accord with this section, you 387 | must place, in the relevant source files, a statement of the 388 | additional terms that apply to those files, or a notice indicating 389 | where to find the applicable terms. 390 | 391 | Additional terms, permissive or non-permissive, may be stated in the 392 | form of a separately written license, or stated as exceptions; 393 | the above requirements apply either way. 394 | 395 | 8. Termination. 396 | 397 | You may not propagate or modify a covered work except as expressly 398 | provided under this License. Any attempt otherwise to propagate or 399 | modify it is void, and will automatically terminate your rights under 400 | this License (including any patent licenses granted under the third 401 | paragraph of section 11). 402 | 403 | However, if you cease all violation of this License, then your 404 | license from a particular copyright holder is reinstated (a) 405 | provisionally, unless and until the copyright holder explicitly and 406 | finally terminates your license, and (b) permanently, if the copyright 407 | holder fails to notify you of the violation by some reasonable means 408 | prior to 60 days after the cessation. 409 | 410 | Moreover, your license from a particular copyright holder is 411 | reinstated permanently if the copyright holder notifies you of the 412 | violation by some reasonable means, this is the first time you have 413 | received notice of violation of this License (for any work) from that 414 | copyright holder, and you cure the violation prior to 30 days after 415 | your receipt of the notice. 416 | 417 | Termination of your rights under this section does not terminate the 418 | licenses of parties who have received copies or rights from you under 419 | this License. If your rights have been terminated and not permanently 420 | reinstated, you do not qualify to receive new licenses for the same 421 | material under section 10. 422 | 423 | 9. Acceptance Not Required for Having Copies. 424 | 425 | You are not required to accept this License in order to receive or 426 | run a copy of the Program. Ancillary propagation of a covered work 427 | occurring solely as a consequence of using peer-to-peer transmission 428 | to receive a copy likewise does not require acceptance. However, 429 | nothing other than this License grants you permission to propagate or 430 | modify any covered work. These actions infringe copyright if you do 431 | not accept this License. Therefore, by modifying or propagating a 432 | covered work, you indicate your acceptance of this License to do so. 433 | 434 | 10. Automatic Licensing of Downstream Recipients. 435 | 436 | Each time you convey a covered work, the recipient automatically 437 | receives a license from the original licensors, to run, modify and 438 | propagate that work, subject to this License. You are not responsible 439 | for enforcing compliance by third parties with this License. 440 | 441 | An "entity transaction" is a transaction transferring control of an 442 | organization, or substantially all assets of one, or subdividing an 443 | organization, or merging organizations. If propagation of a covered 444 | work results from an entity transaction, each party to that 445 | transaction who receives a copy of the work also receives whatever 446 | licenses to the work the party's predecessor in interest had or could 447 | give under the previous paragraph, plus a right to possession of the 448 | Corresponding Source of the work from the predecessor in interest, if 449 | the predecessor has it or can get it with reasonable efforts. 450 | 451 | You may not impose any further restrictions on the exercise of the 452 | rights granted or affirmed under this License. For example, you may 453 | not impose a license fee, royalty, or other charge for exercise of 454 | rights granted under this License, and you may not initiate litigation 455 | (including a cross-claim or counterclaim in a lawsuit) alleging that 456 | any patent claim is infringed by making, using, selling, offering for 457 | sale, or importing the Program or any portion of it. 458 | 459 | 11. Patents. 460 | 461 | A "contributor" is a copyright holder who authorizes use under this 462 | License of the Program or a work on which the Program is based. The 463 | work thus licensed is called the contributor's "contributor version". 464 | 465 | A contributor's "essential patent claims" are all patent claims 466 | owned or controlled by the contributor, whether already acquired or 467 | hereafter acquired, that would be infringed by some manner, permitted 468 | by this License, of making, using, or selling its contributor version, 469 | but do not include claims that would be infringed only as a 470 | consequence of further modification of the contributor version. For 471 | purposes of this definition, "control" includes the right to grant 472 | patent sublicenses in a manner consistent with the requirements of 473 | this License. 474 | 475 | Each contributor grants you a non-exclusive, worldwide, royalty-free 476 | patent license under the contributor's essential patent claims, to 477 | make, use, sell, offer for sale, import and otherwise run, modify and 478 | propagate the contents of its contributor version. 479 | 480 | In the following three paragraphs, a "patent license" is any express 481 | agreement or commitment, however denominated, not to enforce a patent 482 | (such as an express permission to practice a patent or covenant not to 483 | sue for patent infringement). To "grant" such a patent license to a 484 | party means to make such an agreement or commitment not to enforce a 485 | patent against the party. 486 | 487 | If you convey a covered work, knowingly relying on a patent license, 488 | and the Corresponding Source of the work is not available for anyone 489 | to copy, free of charge and under the terms of this License, through a 490 | publicly available network server or other readily accessible means, 491 | then you must either (1) cause the Corresponding Source to be so 492 | available, or (2) arrange to deprive yourself of the benefit of the 493 | patent license for this particular work, or (3) arrange, in a manner 494 | consistent with the requirements of this License, to extend the patent 495 | license to downstream recipients. "Knowingly relying" means you have 496 | actual knowledge that, but for the patent license, your conveying the 497 | covered work in a country, or your recipient's use of the covered work 498 | in a country, would infringe one or more identifiable patents in that 499 | country that you have reason to believe are valid. 500 | 501 | If, pursuant to or in connection with a single transaction or 502 | arrangement, you convey, or propagate by procuring conveyance of, a 503 | covered work, and grant a patent license to some of the parties 504 | receiving the covered work authorizing them to use, propagate, modify 505 | or convey a specific copy of the covered work, then the patent license 506 | you grant is automatically extended to all recipients of the covered 507 | work and works based on it. 508 | 509 | A patent license is "discriminatory" if it does not include within 510 | the scope of its coverage, prohibits the exercise of, or is 511 | conditioned on the non-exercise of one or more of the rights that are 512 | specifically granted under this License. You may not convey a covered 513 | work if you are a party to an arrangement with a third party that is 514 | in the business of distributing software, under which you make payment 515 | to the third party based on the extent of your activity of conveying 516 | the work, and under which the third party grants, to any of the 517 | parties who would receive the covered work from you, a discriminatory 518 | patent license (a) in connection with copies of the covered work 519 | conveyed by you (or copies made from those copies), or (b) primarily 520 | for and in connection with specific products or compilations that 521 | contain the covered work, unless you entered into that arrangement, 522 | or that patent license was granted, prior to 28 March 2007. 523 | 524 | Nothing in this License shall be construed as excluding or limiting 525 | any implied license or other defenses to infringement that may 526 | otherwise be available to you under applicable patent law. 527 | 528 | 12. No Surrender of Others' Freedom. 529 | 530 | If conditions are imposed on you (whether by court order, agreement or 531 | otherwise) that contradict the conditions of this License, they do not 532 | excuse you from the conditions of this License. If you cannot convey a 533 | covered work so as to satisfy simultaneously your obligations under this 534 | License and any other pertinent obligations, then as a consequence you may 535 | not convey it at all. For example, if you agree to terms that obligate you 536 | to collect a royalty for further conveying from those to whom you convey 537 | the Program, the only way you could satisfy both those terms and this 538 | License would be to refrain entirely from conveying the Program. 539 | 540 | 13. Remote Network Interaction; Use with the GNU General Public License. 541 | 542 | Notwithstanding any other provision of this License, if you modify the 543 | Program, your modified version must prominently offer all users 544 | interacting with it remotely through a computer network (if your version 545 | supports such interaction) an opportunity to receive the Corresponding 546 | Source of your version by providing access to the Corresponding Source 547 | from a network server at no charge, through some standard or customary 548 | means of facilitating copying of software. This Corresponding Source 549 | shall include the Corresponding Source for any work covered by version 3 550 | of the GNU General Public License that is incorporated pursuant to the 551 | following paragraph. 552 | 553 | Notwithstanding any other provision of this License, you have 554 | permission to link or combine any covered work with a work licensed 555 | under version 3 of the GNU General Public License into a single 556 | combined work, and to convey the resulting work. The terms of this 557 | License will continue to apply to the part which is the covered work, 558 | but the work with which it is combined will remain governed by version 559 | 3 of the GNU General Public License. 560 | 561 | 14. Revised Versions of this License. 562 | 563 | The Free Software Foundation may publish revised and/or new versions of 564 | the GNU Affero General Public License from time to time. Such new versions 565 | will be similar in spirit to the present version, but may differ in detail to 566 | address new problems or concerns. 567 | 568 | Each version is given a distinguishing version number. If the 569 | Program specifies that a certain numbered version of the GNU Affero General 570 | Public License "or any later version" applies to it, you have the 571 | option of following the terms and conditions either of that numbered 572 | version or of any later version published by the Free Software 573 | Foundation. If the Program does not specify a version number of the 574 | GNU Affero General Public License, you may choose any version ever published 575 | by the Free Software Foundation. 576 | 577 | If the Program specifies that a proxy can decide which future 578 | versions of the GNU Affero General Public License can be used, that proxy's 579 | public statement of acceptance of a version permanently authorizes you 580 | to choose that version for the Program. 581 | 582 | Later license versions may give you additional or different 583 | permissions. However, no additional obligations are imposed on any 584 | author or copyright holder as a result of your choosing to follow a 585 | later version. 586 | 587 | 15. Disclaimer of Warranty. 588 | 589 | THERE IS NO WARRANTY FOR THE PROGRAM, TO THE EXTENT PERMITTED BY 590 | APPLICABLE LAW. EXCEPT WHEN OTHERWISE STATED IN WRITING THE COPYRIGHT 591 | HOLDERS AND/OR OTHER PARTIES PROVIDE THE PROGRAM "AS IS" WITHOUT WARRANTY 592 | OF ANY KIND, EITHER EXPRESSED OR IMPLIED, INCLUDING, BUT NOT LIMITED TO, 593 | THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR 594 | PURPOSE. THE ENTIRE RISK AS TO THE QUALITY AND PERFORMANCE OF THE PROGRAM 595 | IS WITH YOU. SHOULD THE PROGRAM PROVE DEFECTIVE, YOU ASSUME THE COST OF 596 | ALL NECESSARY SERVICING, REPAIR OR CORRECTION. 597 | 598 | 16. Limitation of Liability. 599 | 600 | IN NO EVENT UNLESS REQUIRED BY APPLICABLE LAW OR AGREED TO IN WRITING 601 | WILL ANY COPYRIGHT HOLDER, OR ANY OTHER PARTY WHO MODIFIES AND/OR CONVEYS 602 | THE PROGRAM AS PERMITTED ABOVE, BE LIABLE TO YOU FOR DAMAGES, INCLUDING ANY 603 | GENERAL, SPECIAL, INCIDENTAL OR CONSEQUENTIAL DAMAGES ARISING OUT OF THE 604 | USE OR INABILITY TO USE THE PROGRAM (INCLUDING BUT NOT LIMITED TO LOSS OF 605 | DATA OR DATA BEING RENDERED INACCURATE OR LOSSES SUSTAINED BY YOU OR THIRD 606 | PARTIES OR A FAILURE OF THE PROGRAM TO OPERATE WITH ANY OTHER PROGRAMS), 607 | EVEN IF SUCH HOLDER OR OTHER PARTY HAS BEEN ADVISED OF THE POSSIBILITY OF 608 | SUCH DAMAGES. 609 | 610 | 17. Interpretation of Sections 15 and 16. 611 | 612 | If the disclaimer of warranty and limitation of liability provided 613 | above cannot be given local legal effect according to their terms, 614 | reviewing courts shall apply local law that most closely approximates 615 | an absolute waiver of all civil liability in connection with the 616 | Program, unless a warranty or assumption of liability accompanies a 617 | copy of the Program in return for a fee. 618 | 619 | END OF TERMS AND CONDITIONS 620 | 621 | How to Apply These Terms to Your New Programs 622 | 623 | If you develop a new program, and you want it to be of the greatest 624 | possible use to the public, the best way to achieve this is to make it 625 | free software which everyone can redistribute and change under these terms. 626 | 627 | To do so, attach the following notices to the program. It is safest 628 | to attach them to the start of each source file to most effectively 629 | state the exclusion of warranty; and each file should have at least 630 | the "copyright" line and a pointer to where the full notice is found. 631 | 632 | 633 | Copyright (C) 634 | 635 | This program is free software: you can redistribute it and/or modify 636 | it under the terms of the GNU Affero General Public License as published 637 | by the Free Software Foundation, either version 3 of the License, or 638 | (at your option) any later version. 639 | 640 | This program is distributed in the hope that it will be useful, 641 | but WITHOUT ANY WARRANTY; without even the implied warranty of 642 | MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the 643 | GNU Affero General Public License for more details. 644 | 645 | You should have received a copy of the GNU Affero General Public License 646 | along with this program. If not, see . 647 | 648 | Also add information on how to contact you by electronic and paper mail. 649 | 650 | If your software can interact with users remotely through a computer 651 | network, you should also make sure that it provides a way for users to 652 | get its source. For example, if your program is a web application, its 653 | interface could display a "Source" link that leads users to an archive 654 | of the code. There are many ways you could offer source, and different 655 | solutions will be better for different programs; see section 13 for the 656 | specific requirements. 657 | 658 | You should also get your employer (if you work as a programmer) or school, 659 | if any, to sign a "copyright disclaimer" for the program, if necessary. 660 | For more information on this, and how to apply and follow the GNU AGPL, see 661 | . 662 | -------------------------------------------------------------------------------- /README.md: -------------------------------------------------------------------------------- 1 | # php-tdengine 2 | 3 | ![GitHub Workflow Status (branch)](https://img.shields.io/github/workflow/status/yurunsoft/php-tdengine/ci/master) 4 | [![Php Version](https://img.shields.io/badge/php-%3E=7.4-brightgreen.svg)](https://secure.php.net/) 5 | [![imi License](https://img.shields.io/badge/license-AGPLv3-brightgreen.svg)](https://github.com/yurunsoft/php-tdengine/blob/master/LICENSE) 6 | 7 | 大数据引擎 [TDengine](https://github.com/taosdata/TDengine) 的 PHP 客户端扩展,还支持了 Swoole 协程化。可以运行在传统 PHP 环境和 Swoole 环境下。 8 | 9 | 编写了在三大操作系统下的编译及测试用例,保证代码的稳定性和可靠性。 10 | 11 | ## 环境要求 12 | 13 | Windows、Linux、MacOS 14 | 15 | PHP >= 7.4 16 | 17 | TDengine >= 2.0(已支持 TDengine 3.0) 18 | 19 | Swoole >= 4.8 (可选) 20 | 21 | ## 编译安装 22 | 23 | 编译前请先安装 [TDengine-client](https://www.taosdata.com/cn/getting-started/#%E9%80%9A%E8%BF%87%E5%AE%89%E8%A3%85%E5%8C%85%E5%AE%89%E8%A3%85) 或者自行编译 TDengine,详见 TDengine 文档: 24 | 25 | **非 Swoole 环境:** 26 | 27 | ```shell 28 | phpize && ./configure && make -j && make install 29 | ``` 30 | 31 | **手动指定 tdengine 目录:** 32 | 33 | ```shell 34 | phpize && ./configure --with-tdengine-dir=/usr/local/Cellar/tdengine/2.4.0.0 && make -j && make install 35 | ``` 36 | 37 | > `--with-tdengine-dir=` 后跟上 tdengine 目录。 38 | > 适用于默认找不到的情况,或者 MacOS 系统用户。 39 | 40 | **Swoole 环境:** 41 | 42 | ```shell 43 | phpize && ./configure --enable-swoole && make -j && make install 44 | ``` 45 | 46 | **启用扩展:** 47 | 48 | 方法一:在 `php.ini` 中加入 `extension=tdengine` 49 | 50 | 方法二:运行带参数 `php -dextension=tdengine test.php` 51 | 52 | ## PHP 代码编写 53 | 54 | > 所有错误都会抛出异常: `TDengine\Exception\TDengineException` 55 | 56 | **基本:** 57 | 58 | ```php 59 | use TDengine\Connection; 60 | 61 | // 获取扩展版本号 62 | var_dump(\TDengine\EXTENSION_VERSION); 63 | 64 | // 设置客户端选项 65 | \TDengine\setOptions([ 66 | \TDengine\TSDB_OPTION_LOCALE => 'en_US.UTF-8', // 区域 67 | \TDengine\TSDB_OPTION_CHARSET => 'UTF-8', // 字符集 68 | \TDengine\TSDB_OPTION_TIMEZONE => 'Asia/Shanghai', // 时区 69 | \TDengine\TSDB_OPTION_CONFIGDIR => '/etc/taos', // 配置目录 70 | \TDengine\TSDB_OPTION_SHELL_ACTIVITY_TIMER => 3, // shell 活动定时器 71 | ]); 72 | 73 | // 获取客户端版本信息 74 | var_dump(\TDengine\CLIENT_VERSION); 75 | var_dump(\TDengine\getClientInfo()); 76 | 77 | // 以下值都是默认值,不改可以不传 78 | $host = '127.0.0.1'; 79 | $port = 6030; 80 | $user = 'root'; 81 | $pass = 'taosdata'; 82 | $db = null; 83 | 84 | // 实例化 85 | $connection = new Connection($host, $port, $user, $pass, $db); 86 | // 连接 87 | $connection->connect(); 88 | // 获取连接参数 89 | $connection->getHost(); 90 | $connection->getPort(); 91 | $connection->getUser(); 92 | $connection->getPass(); 93 | $connection->getDb(); 94 | // 获取服务端信息 95 | $connection->getServerInfo(); 96 | // 选择默认数据库 97 | $connection->selectDb('db1'); 98 | // 关闭连接 99 | $connection->close(); 100 | ``` 101 | 102 | **查询:** 103 | 104 | ```php 105 | // 查询 106 | $resource = $connection->query($sql); // 支持查询和插入 107 | // 获取结果集时间戳字段的精度,0 代表毫秒,1 代表微秒,2 代表纳秒 108 | $resource->getResultPrecision(); 109 | // 获取所有数据 110 | $resource->fetch(); 111 | // 获取一行数据 112 | $resource->fetchRow(); 113 | // 获取字段数组 114 | $resource->fetchFields(); 115 | // 获取列数 116 | $resource->getFieldCount(); 117 | // 获取影响行数 118 | $resource->affectedRows(); 119 | // 获取 SQL 语句 120 | $resource->getSql(); 121 | // 获取连接对象 122 | $resource->getConnection(); 123 | // 关闭资源(一般不需要手动关闭,变量销毁时会自动释放) 124 | $resource->close(); 125 | ``` 126 | 127 | **参数绑定:** 128 | 129 | ```php 130 | // 查询 131 | $stmt = $connection->prepare($sql); // 支持查询和插入,参数用?占位 132 | // 设置表名和标签 133 | $stmt->setTableNameTags('表名', [ 134 | // 支持格式同参数绑定 135 | [TDengine\TSDB_DATA_TYPE_INT, 36], 136 | ]); 137 | // 绑定参数方法1 138 | $stmt->bindParams( 139 | // [字段类型, 值] 140 | [TDengine\TSDB_DATA_TYPE_TIMESTAMP, $time1], 141 | [TDengine\TSDB_DATA_TYPE_INT, 36], 142 | [TDengine\TSDB_DATA_TYPE_FLOAT, 44.0], 143 | ); 144 | // 绑定参数方法2 145 | $stmt->bindParams([ 146 | // ['type' => 字段类型, 'value' => 值] 147 | ['type' => TDengine\TSDB_DATA_TYPE_TIMESTAMP, 'value' => $time2], 148 | ['type' => TDengine\TSDB_DATA_TYPE_INT, 'value' => 36], 149 | ['type' => TDengine\TSDB_DATA_TYPE_FLOAT, 'value' => 44.0], 150 | ]); 151 | // 执行 SQL,返回 Resource,使用方法同 query() 返回值 152 | $resource = $stmt->execute(); 153 | // 获取 SQL 语句 154 | $stmt->getSql(); 155 | // 获取连接对象 156 | $stmt->getConnection(); 157 | // 关闭(一般不需要手动关闭,变量销毁时会自动释放) 158 | $stmt->close(); 159 | ``` 160 | 161 | **字段类型:** 162 | 163 | | 参数名称 | 说明 | 164 | | ------------ | ------------ 165 | | `TDengine\TSDB_DATA_TYPE_NULL` | null | 166 | | `TDengine\TSDB_DATA_TYPE_BOOL` | bool | 167 | | `TDengine\TSDB_DATA_TYPE_TINYINT` | tinyint | 168 | | `TDengine\TSDB_DATA_TYPE_SMALLINT` | smallint | 169 | | `TDengine\TSDB_DATA_TYPE_INT` | int | 170 | | `TDengine\TSDB_DATA_TYPE_BIGINT` | bigint | 171 | | `TDengine\TSDB_DATA_TYPE_FLOAT` | float | 172 | | `TDengine\TSDB_DATA_TYPE_DOUBLE` | double | 173 | | `TDengine\TSDB_DATA_TYPE_BINARY` | binary | 174 | | `TDengine\TSDB_DATA_TYPE_TIMESTAMP` | timestamp | 175 | | `TDengine\TSDB_DATA_TYPE_NCHAR` | nchar | 176 | | `TDengine\TSDB_DATA_TYPE_UTINYINT` | utinyint | 177 | | `TDengine\TSDB_DATA_TYPE_USMALLINT` | usmallint | 178 | | `TDengine\TSDB_DATA_TYPE_UINT` | uint | 179 | | `TDengine\TSDB_DATA_TYPE_UBIGINT` | ubigint | 180 | | `TDengine\TSDB_DATA_TYPE_JSON` | json | 181 | | `TDengine\TSDB_DATA_TYPE_VARBINARY` | varbinary | 182 | | `TDengine\TSDB_DATA_TYPE_DECIMAL` | decimal | 183 | | `TDengine\TSDB_DATA_TYPE_BLOB` | blob | 184 | | `TDengine\TSDB_DATA_TYPE_MEDIUMBLOB` | mediumblob | 185 | | `TDengine\TSDB_DATA_TYPE_BINARY` | binary | 186 | -------------------------------------------------------------------------------- /config.m4: -------------------------------------------------------------------------------- 1 | dnl config.m4 for extension tdengine 2 | 3 | dnl Comments in this file start with the string 'dnl'. 4 | dnl Remove where necessary. 5 | 6 | dnl If your extension references something external, use 'with': 7 | 8 | dnl PHP_ARG_WITH([tdengine], 9 | dnl [for tdengine support], 10 | dnl [AS_HELP_STRING([--with-tdengine], 11 | dnl [Include tdengine support])]) 12 | 13 | dnl Otherwise use 'enable': 14 | 15 | PHP_ARG_ENABLE([tdengine], 16 | [whether to enable tdengine support], 17 | [AS_HELP_STRING([--enable-tdengine], 18 | [Enable tdengine support])], 19 | [no]) 20 | 21 | PHP_ARG_ENABLE(swoole, swoole support, 22 | [ --enable-swoole Enable swoole support], [enable_swoole="yes"]) 23 | 24 | if test "$PHP_TDENGINE" != "no"; then 25 | dnl Write more examples of tests here... 26 | 27 | dnl Remove this code block if the library does not support pkg-config. 28 | dnl PKG_CHECK_MODULES([LIBFOO], [foo]) 29 | dnl PHP_EVAL_INCLINE($LIBFOO_CFLAGS) 30 | dnl PHP_EVAL_LIBLINE($LIBFOO_LIBS, TDENGINE_SHARED_LIBADD) 31 | 32 | dnl If you need to check for a particular library version using PKG_CHECK_MODULES, 33 | dnl you can use comparison operators. For example: 34 | dnl PKG_CHECK_MODULES([LIBFOO], [foo >= 1.2.3]) 35 | dnl PKG_CHECK_MODULES([LIBFOO], [foo < 3.4]) 36 | dnl PKG_CHECK_MODULES([LIBFOO], [foo = 1.2.3]) 37 | 38 | dnl Remove this code block if the library supports pkg-config. 39 | dnl --with-tdengine -> check with-path 40 | dnl SEARCH_PATH="/usr/local /usr" # you might want to change this 41 | dnl SEARCH_FOR="/include/tdengine.h" # you most likely want to change this 42 | dnl if test -r $PHP_TDENGINE/$SEARCH_FOR; then # path given as parameter 43 | dnl TDENGINE_DIR=$PHP_TDENGINE 44 | dnl else # search default path list 45 | dnl AC_MSG_CHECKING([for tdengine files in default path]) 46 | dnl for i in $SEARCH_PATH ; do 47 | dnl if test -r $i/$SEARCH_FOR; then 48 | dnl TDENGINE_DIR=$i 49 | dnl AC_MSG_RESULT(found in $i) 50 | dnl fi 51 | dnl done 52 | dnl fi 53 | dnl 54 | dnl if test -z "$TDENGINE_DIR"; then 55 | dnl AC_MSG_RESULT([not found]) 56 | dnl AC_MSG_ERROR([Please reinstall the tdengine distribution]) 57 | dnl fi 58 | 59 | dnl Remove this code block if the library supports pkg-config. 60 | dnl --with-tdengine -> add include path 61 | dnl PHP_ADD_INCLUDE($TDENGINE_DIR/include) 62 | 63 | dnl Remove this code block if the library supports pkg-config. 64 | dnl --with-tdengine -> check for lib and symbol presence 65 | dnl LIBNAME=TDENGINE # you may want to change this 66 | dnl LIBSYMBOL=TDENGINE # you most likely want to change this 67 | 68 | dnl If you need to check for a particular library function (e.g. a conditional 69 | dnl or version-dependent feature) and you are using pkg-config: 70 | dnl PHP_CHECK_LIBRARY($LIBNAME, $LIBSYMBOL, 71 | dnl [ 72 | dnl AC_DEFINE(HAVE_TDENGINE_FEATURE, 1, [ ]) 73 | dnl ],[ 74 | dnl AC_MSG_ERROR([FEATURE not supported by your tdengine library.]) 75 | dnl ], [ 76 | dnl $LIBFOO_LIBS 77 | dnl ]) 78 | 79 | dnl If you need to check for a particular library function (e.g. a conditional 80 | dnl or version-dependent feature) and you are not using pkg-config: 81 | dnl PHP_CHECK_LIBRARY($LIBNAME, $LIBSYMBOL, 82 | dnl [ 83 | dnl PHP_ADD_LIBRARY_WITH_PATH($LIBNAME, $TDENGINE_DIR/$PHP_LIBDIR, TDENGINE_SHARED_LIBADD) 84 | dnl AC_DEFINE(HAVE_TDENGINE_FEATURE, 1, [ ]) 85 | dnl ],[ 86 | dnl AC_MSG_ERROR([FEATURE not supported by your tdengine library.]) 87 | dnl ],[ 88 | dnl -L$TDENGINE_DIR/$PHP_LIBDIR -lm 89 | dnl ]) 90 | dnl 91 | dnl PHP_SUBST(TDENGINE_SHARED_LIBADD) 92 | 93 | dnl In case of no dependencies 94 | PHP_ARG_WITH([tdengine_dir], 95 | [dir of tdengine], 96 | [AS_HELP_STRING([[--with-tdengine-dir[=DIR]]], 97 | [Include TDengine support (requires TDengine >= 2.0.0)])], [no], [no]) 98 | 99 | AC_DEFINE(HAVE_TDENGINE, 1, [ Have tdengine support ]) 100 | if test "$PHP_TDENGINE_DIR" != "no"; then 101 | TDENGINE_INCLUDE="${PHP_TDENGINE_DIR}/include" 102 | TDENGINE_LIBDIR="${PHP_TDENGINE_DIR}/driver" 103 | else 104 | TDENGINE_INCLUDE="/usr/local/taos/include" 105 | TDENGINE_LIBDIR="/usr/local/taos/driver" 106 | fi 107 | 108 | PHP_CHECK_LIBRARY(taos, taos_init, 109 | [ 110 | ], [ 111 | AC_MSG_ERROR(tdengine module requires libtaos >= 2.0.0) 112 | ], [ 113 | -L$TDENGINE_LIBDIR 114 | ]) 115 | 116 | AC_CHECK_TYPES([TAOS_BIND], [ 117 | AC_DEFINE(HAVE_TAOS_BIND, 1, [ Have TAOS_BIND ]) 118 | ], [], [#include ]) 119 | 120 | PHP_ADD_LIBRARY_WITH_PATH(taos, $TDENGINE_LIBDIR, TDENGINE_SHARED_LIBADD) 121 | PHP_SUBST(TDENGINE_SHARED_LIBADD) 122 | 123 | PHP_ADD_INCLUDE($TDENGINE_INCLUDE) 124 | 125 | if test "$PHP_SWOOLE" = "yes"; then 126 | AC_DEFINE(HAVE_SWOOLE, 1, [use swoole]) 127 | PHP_ADD_INCLUDE([$phpincludedir/ext/swoole]) 128 | PHP_ADD_INCLUDE([$phpincludedir/ext/swoole/include]) 129 | PHP_ADD_EXTENSION_DEP(tdengine, swoole) 130 | fi 131 | 132 | PHP_NEW_EXTENSION(tdengine, tdengine.cc src/ext_taos.cc src/ext_taos_connection.cc src/ext_taos_resource.cc src/ext_taos_statement.cc, $ext_shared,,, cxx) 133 | 134 | PHP_REQUIRE_CXX() 135 | 136 | CXXFLAGS="$CXXFLAGS -Wall -Wno-unused-function -Wno-deprecated -Wno-deprecated-declarations -Wwrite-strings -std=c++11" 137 | fi 138 | -------------------------------------------------------------------------------- /config.w32: -------------------------------------------------------------------------------- 1 | ARG_ENABLE('tdengine', 'tdengine support', 'yes'); 2 | 3 | function checkTaosTstrerror() 4 | { 5 | var FSO = WScript.CreateObject("Scripting.FileSystemObject"); 6 | if (header = FSO.OpenTextFile(PHP_TDENGINE_DIR + '\\include\\taoserror.h', 1)) { 7 | contents = header.ReadAll(); 8 | header.Close(); 9 | return contents.indexOf('DLL_EXPORT const char* tstrerror') >= 0; 10 | } 11 | return false; 12 | } 13 | 14 | if (PHP_TDENGINE != 'no') { 15 | AC_DEFINE('HAVE_TDENGINE', 1, 'tdengine support enabled'); 16 | 17 | ARG_WITH('tdengine_dir', 'Include TDengine support (requires TDengine >= 2.0.0)', 'C:\\TDengine'); 18 | CHECK_LIB('taos.lib', 'tdengine', PHP_TDENGINE_DIR + '\\driver'); 19 | CHECK_HEADER_ADD_INCLUDE('taos.h', 'CFLAGS_TDENGINE', PHP_TDENGINE_DIR + '\\include'); 20 | CHECK_FUNC_IN_HEADER('taos.h', 'TAOS_BIND', PHP_TDENGINE_DIR + '\\include') 21 | 22 | if (!checkTaosTstrerror()) 23 | { 24 | AC_DEFINE('NO_TSTRERROR', 1, "Detected compiler version"); 25 | } 26 | 27 | ADD_FLAG('CFLAGS_TDENGINE', '/I "' + configure_module_dirname + '"'); 28 | ADD_FLAG('CFLAGS_TDENGINE', '/I "' + configure_module_dirname + '\\include"'); 29 | 30 | var shared = PHP_TDENGINE; 31 | if(PHP_TDENGINE.indexOf('static') >= 0){ 32 | shared = null; 33 | } 34 | 35 | ADD_SOURCES(configure_module_dirname + '\\src', [ 36 | 'ext_taos.cc', 37 | 'ext_taos_connection.cc', 38 | 'ext_taos_resource.cc', 39 | 'ext_taos_statement.cc', 40 | ].join(' '), "tdengine"); 41 | 42 | if (shared) 43 | { 44 | ADD_FLAG("CFLAGS_TDENGINE", '/D PHP_TDENGINE_EXPORTS'); 45 | } 46 | 47 | EXTENSION('tdengine', 'tdengine.cc', shared, '/DZEND_ENABLE_STATIC_TSRMLS_CACHE=1', 'php_tdengine.dll'); 48 | } 49 | -------------------------------------------------------------------------------- /include/ext_taos.h: -------------------------------------------------------------------------------- 1 | #ifndef PHP_EXT_TAOS_H 2 | # define PHP_EXT_TAOS_H 3 | 4 | #include "php_tdengine.h" 5 | #include "ext_tdengine.h" 6 | 7 | // TDengine functions 8 | PHP_FUNCTION(setOptions); 9 | PHP_FUNCTION(getClientInfo); 10 | const zend_function_entry ext_functions[] = { 11 | ZEND_NS_FE("TDengine", setOptions, arginfo_TDengine_setOptions) 12 | ZEND_NS_FE("TDengine", getClientInfo, arginfo_TDengine_getClientInfo) 13 | ZEND_FE_END 14 | }; 15 | 16 | inline void register_constants(int module_number) 17 | { 18 | // version 19 | REGISTER_NS_STRING_CONSTANT("TDengine", "EXTENSION_VERSION", PHP_TDENGINE_VERSION, CONST_CS | CONST_PERSISTENT); 20 | REGISTER_NS_STRING_CONSTANT("TDengine", "CLIENT_VERSION", (char*) taos_get_client_info(), CONST_CS | CONST_PERSISTENT); 21 | 22 | // option 23 | REGISTER_NS_LONG_CONSTANT("TDengine", "TSDB_OPTION_LOCALE", TSDB_OPTION_LOCALE, CONST_CS | CONST_PERSISTENT); 24 | REGISTER_NS_LONG_CONSTANT("TDengine", "TSDB_OPTION_CHARSET", TSDB_OPTION_CHARSET, CONST_CS | CONST_PERSISTENT); 25 | REGISTER_NS_LONG_CONSTANT("TDengine", "TSDB_OPTION_TIMEZONE", TSDB_OPTION_TIMEZONE, CONST_CS | CONST_PERSISTENT); 26 | REGISTER_NS_LONG_CONSTANT("TDengine", "TSDB_OPTION_CONFIGDIR", TSDB_OPTION_CONFIGDIR, CONST_CS | CONST_PERSISTENT); 27 | REGISTER_NS_LONG_CONSTANT("TDengine", "TSDB_OPTION_SHELL_ACTIVITY_TIMER", TSDB_OPTION_SHELL_ACTIVITY_TIMER, CONST_CS | CONST_PERSISTENT); 28 | 29 | // data type 30 | REGISTER_NS_LONG_CONSTANT("TDengine", "TSDB_DATA_TYPE_NULL", TSDB_DATA_TYPE_NULL, CONST_CS | CONST_PERSISTENT); 31 | REGISTER_NS_LONG_CONSTANT("TDengine", "TSDB_DATA_TYPE_BOOL", TSDB_DATA_TYPE_BOOL, CONST_CS | CONST_PERSISTENT); 32 | REGISTER_NS_LONG_CONSTANT("TDengine", "TSDB_DATA_TYPE_TINYINT", TSDB_DATA_TYPE_TINYINT, CONST_CS | CONST_PERSISTENT); 33 | REGISTER_NS_LONG_CONSTANT("TDengine", "TSDB_DATA_TYPE_SMALLINT", TSDB_DATA_TYPE_SMALLINT, CONST_CS | CONST_PERSISTENT); 34 | REGISTER_NS_LONG_CONSTANT("TDengine", "TSDB_DATA_TYPE_INT", TSDB_DATA_TYPE_INT, CONST_CS | CONST_PERSISTENT); 35 | REGISTER_NS_LONG_CONSTANT("TDengine", "TSDB_DATA_TYPE_BIGINT", TSDB_DATA_TYPE_BIGINT, CONST_CS | CONST_PERSISTENT); 36 | REGISTER_NS_LONG_CONSTANT("TDengine", "TSDB_DATA_TYPE_FLOAT", TSDB_DATA_TYPE_FLOAT, CONST_CS | CONST_PERSISTENT); 37 | REGISTER_NS_LONG_CONSTANT("TDengine", "TSDB_DATA_TYPE_DOUBLE", TSDB_DATA_TYPE_DOUBLE, CONST_CS | CONST_PERSISTENT); 38 | REGISTER_NS_LONG_CONSTANT("TDengine", "TSDB_DATA_TYPE_BINARY", TSDB_DATA_TYPE_BINARY, CONST_CS | CONST_PERSISTENT); 39 | REGISTER_NS_LONG_CONSTANT("TDengine", "TSDB_DATA_TYPE_TIMESTAMP", TSDB_DATA_TYPE_TIMESTAMP, CONST_CS | CONST_PERSISTENT); 40 | REGISTER_NS_LONG_CONSTANT("TDengine", "TSDB_DATA_TYPE_NCHAR", TSDB_DATA_TYPE_NCHAR, CONST_CS | CONST_PERSISTENT); 41 | #ifdef TSDB_DATA_TYPE_UTINYINT 42 | REGISTER_NS_LONG_CONSTANT("TDengine", "TSDB_DATA_TYPE_UTINYINT", TSDB_DATA_TYPE_UTINYINT, CONST_CS | CONST_PERSISTENT); 43 | #endif 44 | #ifdef TSDB_DATA_TYPE_USMALLINT 45 | REGISTER_NS_LONG_CONSTANT("TDengine", "TSDB_DATA_TYPE_USMALLINT", TSDB_DATA_TYPE_USMALLINT, CONST_CS | CONST_PERSISTENT); 46 | #endif 47 | #ifdef TSDB_DATA_TYPE_UINT 48 | REGISTER_NS_LONG_CONSTANT("TDengine", "TSDB_DATA_TYPE_UINT", TSDB_DATA_TYPE_UINT, CONST_CS | CONST_PERSISTENT); 49 | #endif 50 | #ifdef TSDB_DATA_TYPE_UBIGINT 51 | REGISTER_NS_LONG_CONSTANT("TDengine", "TSDB_DATA_TYPE_UBIGINT", TSDB_DATA_TYPE_UBIGINT, CONST_CS | CONST_PERSISTENT); 52 | #endif 53 | #ifdef TSDB_DATA_TYPE_JSON 54 | REGISTER_NS_LONG_CONSTANT("TDengine", "TSDB_DATA_TYPE_JSON", TSDB_DATA_TYPE_JSON, CONST_CS | CONST_PERSISTENT); 55 | #endif 56 | #ifdef TSDB_DATA_TYPE_VARBINARY 57 | REGISTER_NS_LONG_CONSTANT("TDengine", "TSDB_DATA_TYPE_VARBINARY", TSDB_DATA_TYPE_VARBINARY, CONST_CS | CONST_PERSISTENT); 58 | #endif 59 | #ifdef TSDB_DATA_TYPE_DECIMAL 60 | REGISTER_NS_LONG_CONSTANT("TDengine", "TSDB_DATA_TYPE_DECIMAL", TSDB_DATA_TYPE_DECIMAL, CONST_CS | CONST_PERSISTENT); 61 | #endif 62 | #ifdef TSDB_DATA_TYPE_BLOB 63 | REGISTER_NS_LONG_CONSTANT("TDengine", "TSDB_DATA_TYPE_BLOB", TSDB_DATA_TYPE_BLOB, CONST_CS | CONST_PERSISTENT); 64 | #endif 65 | #ifdef TSDB_DATA_TYPE_MEDIUMBLOB 66 | REGISTER_NS_LONG_CONSTANT("TDengine", "TSDB_DATA_TYPE_MEDIUMBLOB", TSDB_DATA_TYPE_MEDIUMBLOB, CONST_CS | CONST_PERSISTENT); 67 | #endif 68 | } 69 | 70 | #endif /* PHP_EXT_TAOS_H */ 71 | -------------------------------------------------------------------------------- /include/ext_taos_connection.h: -------------------------------------------------------------------------------- 1 | #ifndef PHP_EXT_TAOS_CONNECTION_H 2 | # define PHP_EXT_TAOS_CONNECTION_H 3 | #include "ext_tdengine.h" 4 | 5 | #define check_connected(connection) \ 6 | if (!is_connected(connection)) \ 7 | { \ 8 | zend_throw_exception_ex(TDengine_Exception_ce, 0, "Not connected"); \ 9 | RETURN_THROWS(); \ 10 | } 11 | 12 | #define throw_taos_exception(message, errno) \ 13 | zend_throw_exception_ex(TDengine_Exception_ce, errno, "%s", message); \ 14 | RETURN_THROWS(); 15 | 16 | #define throw_taos_exception_by_connection(_connection) \ 17 | zend_throw_exception_ex(TDengine_Exception_ce, taos_errno(_connection->connection), "%s", taos_errstr(_connection->connection)); \ 18 | RETURN_THROWS(); 19 | 20 | #ifdef NO_TSTRERROR 21 | #define throw_taos_exception_by_errno(errno) \ 22 | zend_throw_exception_ex(TDengine_Exception_ce, errno, "%s", inner_tstrerror(errno)); \ 23 | RETURN_THROWS(); 24 | #else 25 | #define throw_taos_exception_by_errno(errno) \ 26 | zend_throw_exception_ex(TDengine_Exception_ce, errno, "%s", tstrerror(errno)); \ 27 | RETURN_THROWS(); 28 | #endif 29 | 30 | typedef struct { 31 | const char *host; 32 | int port; 33 | const char *user; 34 | const char *pass; 35 | const char *db; 36 | TAOS *connection; 37 | } TDengineConnection; 38 | 39 | inline int is_connected(TDengineConnection *connection) 40 | { 41 | return connection->connection ? 1 : 0; 42 | } 43 | 44 | inline void connection_close(TDengineConnection *connection) 45 | { 46 | taos_close(connection->connection); 47 | connection->connection = nullptr; 48 | } 49 | 50 | // TDengine\Connection 51 | ZEND_METHOD(TDengine_Connection, __construct); 52 | ZEND_METHOD(TDengine_Connection, connect); 53 | ZEND_METHOD(TDengine_Connection, close); 54 | ZEND_METHOD(TDengine_Connection, getHost); 55 | ZEND_METHOD(TDengine_Connection, getPort); 56 | ZEND_METHOD(TDengine_Connection, getUser); 57 | ZEND_METHOD(TDengine_Connection, getPass); 58 | ZEND_METHOD(TDengine_Connection, getDb); 59 | ZEND_METHOD(TDengine_Connection, getServerInfo); 60 | ZEND_METHOD(TDengine_Connection, selectDb); 61 | ZEND_METHOD(TDengine_Connection, query); 62 | ZEND_METHOD(TDengine_Connection, prepare); 63 | ZEND_METHOD(TDengine_Connection, isConnected); 64 | 65 | typedef struct { 66 | TDengineConnection *ptr; 67 | zend_object std; 68 | } ConnectionObject; 69 | 70 | const zend_function_entry class_TDengine_Connection_methods[] = { 71 | ZEND_ME(TDengine_Connection, __construct, arginfo_class_TDengine_Connection___construct, ZEND_ACC_PUBLIC) 72 | ZEND_ME(TDengine_Connection, connect, arginfo_class_TDengine_Connection_connect, ZEND_ACC_PUBLIC) 73 | ZEND_ME(TDengine_Connection, close, arginfo_class_TDengine_Connection_close, ZEND_ACC_PUBLIC) 74 | ZEND_ME(TDengine_Connection, getHost, arginfo_class_TDengine_Connection_getHost, ZEND_ACC_PUBLIC) 75 | ZEND_ME(TDengine_Connection, getPort, arginfo_class_TDengine_Connection_getPort, ZEND_ACC_PUBLIC) 76 | ZEND_ME(TDengine_Connection, getUser, arginfo_class_TDengine_Connection_getUser, ZEND_ACC_PUBLIC) 77 | ZEND_ME(TDengine_Connection, getPass, arginfo_class_TDengine_Connection_getPass, ZEND_ACC_PUBLIC) 78 | ZEND_ME(TDengine_Connection, getDb, arginfo_class_TDengine_Connection_getDb, ZEND_ACC_PUBLIC) 79 | ZEND_ME(TDengine_Connection, getServerInfo, arginfo_class_TDengine_Connection_getServerInfo, ZEND_ACC_PUBLIC) 80 | ZEND_ME(TDengine_Connection, selectDb, arginfo_class_TDengine_Connection_selectDb, ZEND_ACC_PUBLIC) 81 | ZEND_ME(TDengine_Connection, query, arginfo_class_TDengine_Connection_query, ZEND_ACC_PUBLIC) 82 | ZEND_ME(TDengine_Connection, prepare, arginfo_class_TDengine_Connection_prepare, ZEND_ACC_PUBLIC) 83 | ZEND_ME(TDengine_Connection, isConnected, arginfo_class_TDengine_Connection_isConnected, ZEND_ACC_PUBLIC) 84 | ZEND_FE_END 85 | }; 86 | 87 | extern PHP_TDENGINE_API zend_class_entry *TDengine_Connection_ce; 88 | extern PHP_TDENGINE_API zend_object_handlers tdengine_connection_handlers; 89 | 90 | extern PHP_TDENGINE_API bool taos_inited; 91 | 92 | inline zend_object *php_tdengine_connection_create_object(zend_class_entry *ce) { 93 | if (!taos_inited) 94 | { 95 | std::thread([]() { 96 | #if !IS_WIN 97 | sigset_t mask; 98 | sigfillset(&mask); 99 | pthread_sigmask(SIG_BLOCK, &mask, nullptr); 100 | #endif 101 | taos_init(); 102 | }).join(); 103 | taos_inited = true; 104 | } 105 | ConnectionObject *obj = (ConnectionObject *) zend_object_alloc(sizeof(ConnectionObject), ce); 106 | TDengineConnection *connection = (TDengineConnection*) emalloc(sizeof(TDengineConnection)); 107 | connection->host = nullptr; 108 | connection->port = 6030; 109 | connection->user = nullptr; 110 | connection->pass = nullptr; 111 | connection->db = nullptr; 112 | connection->connection = nullptr; 113 | zend_object *zobj = &obj->std; 114 | obj->ptr = connection; 115 | zend_object_std_init(zobj, ce); 116 | object_properties_init(zobj, ce); 117 | zobj->handlers = &tdengine_connection_handlers; 118 | return zobj; 119 | } 120 | 121 | inline void php_tdengine_connection_free_object(zend_object *zobj) { 122 | ConnectionObject *obj = zend_object_to_object(zobj, ConnectionObject); 123 | 124 | if (obj && obj->ptr) 125 | { 126 | TDengineConnection *connection = obj->ptr; 127 | if (connection && is_connected(connection)) 128 | { 129 | connection_close(connection); 130 | } 131 | 132 | efree(connection); 133 | obj->ptr = nullptr; 134 | } 135 | 136 | zend_object_std_dtor(zobj); 137 | } 138 | 139 | inline zend_class_entry *register_class_TDengine_Connection(void) 140 | { 141 | zend_class_entry ce; 142 | 143 | INIT_NS_CLASS_ENTRY(ce, "TDengine", "Connection", class_TDengine_Connection_methods); 144 | TDengine_Connection_ce = zend_register_internal_class_ex(&ce, nullptr); 145 | TDengine_Connection_ce->create_object = php_tdengine_connection_create_object; 146 | memcpy(&tdengine_connection_handlers, &std_object_handlers, sizeof(tdengine_connection_handlers)); 147 | tdengine_connection_handlers.offset = XtOffsetOf(ConnectionObject, std); 148 | tdengine_connection_handlers.free_obj = php_tdengine_connection_free_object; 149 | 150 | return TDengine_Connection_ce; 151 | } 152 | 153 | // TDengine\Exception\TDengineException 154 | extern PHP_TDENGINE_API zend_class_entry *TDengine_Exception_ce; 155 | 156 | const zend_function_entry class_TDengine_Exception_methods[] = { 157 | ZEND_FE_END 158 | }; 159 | 160 | inline zend_class_entry *register_class_TDengine_Exception(zend_class_entry *class_entry_Exception) 161 | { 162 | zend_class_entry ce; 163 | 164 | INIT_NS_CLASS_ENTRY(ce, "TDengine\\Exception", "TDengineException", class_TDengine_Exception_methods); 165 | TDengine_Exception_ce = zend_register_internal_class_ex(&ce, class_entry_Exception); 166 | 167 | return TDengine_Exception_ce; 168 | } 169 | 170 | #endif /* PHP_EXT_TAOS_CONNECTION_H */ -------------------------------------------------------------------------------- /include/ext_taos_resource.h: -------------------------------------------------------------------------------- 1 | #ifndef PHP_EXT_TAOS_RESOURCE_H 2 | # define PHP_EXT_TAOS_RESOURCE_H 3 | #include "ext_taos_connection.h" 4 | #include "ext_taos_statement.h" 5 | 6 | typedef struct { 7 | TAOS_RES *res; 8 | ConnectionObject *connection; 9 | StatementObject *statement; 10 | const char *sql; 11 | } TDengineResource; 12 | 13 | typedef struct { 14 | TDengineResource *ptr; 15 | zend_object std; 16 | } ResourceObject; 17 | 18 | inline void close_resource(TDengineResource *resource) 19 | { 20 | if (resource->res) 21 | { 22 | taos_stop_query(resource->res); 23 | taos_free_result(resource->res); 24 | resource->res = nullptr; 25 | } 26 | } 27 | 28 | bool fetch_row(zval *zrow, TDengineResource *resource, TAOS_FIELD *fields, int field_count); 29 | void fetch(zval *zv, TDengineResource *resource); 30 | void fetch_fields(zval *zresult, TDengineResource *resource); 31 | 32 | // TDengine\Resource 33 | ZEND_METHOD(TDengine_Resource, getConnection); 34 | ZEND_METHOD(TDengine_Resource, getStatement); 35 | ZEND_METHOD(TDengine_Resource, getSql); 36 | ZEND_METHOD(TDengine_Resource, getResultPrecision); 37 | ZEND_METHOD(TDengine_Resource, fetch); 38 | ZEND_METHOD(TDengine_Resource, fetchRow); 39 | ZEND_METHOD(TDengine_Resource, getFieldCount); 40 | ZEND_METHOD(TDengine_Resource, affectedRows); 41 | ZEND_METHOD(TDengine_Resource, fetchFields); 42 | ZEND_METHOD(TDengine_Resource, close); 43 | 44 | const zend_function_entry class_TDengine_Resource_methods[] = { 45 | ZEND_ME(TDengine_Resource, getConnection, arginfo_class_TDengine_Resource_getConnection, ZEND_ACC_PUBLIC) 46 | ZEND_ME(TDengine_Resource, getStatement, arginfo_class_TDengine_Resource_getStatement, ZEND_ACC_PUBLIC) 47 | ZEND_ME(TDengine_Resource, getSql, arginfo_class_TDengine_Resource_getSql, ZEND_ACC_PUBLIC) 48 | ZEND_ME(TDengine_Resource, getResultPrecision, arginfo_class_TDengine_Resource_getResultPrecision, ZEND_ACC_PUBLIC) 49 | ZEND_ME(TDengine_Resource, fetch, arginfo_class_TDengine_Resource_fetch, ZEND_ACC_PUBLIC) 50 | ZEND_ME(TDengine_Resource, fetchRow, arginfo_class_TDengine_Resource_fetchRow, ZEND_ACC_PUBLIC) 51 | ZEND_ME(TDengine_Resource, getFieldCount, arginfo_class_TDengine_Resource_getFieldCount, ZEND_ACC_PUBLIC) 52 | ZEND_ME(TDengine_Resource, affectedRows, arginfo_class_TDengine_Resource_affectedRows, ZEND_ACC_PUBLIC) 53 | ZEND_ME(TDengine_Resource, fetchFields, arginfo_class_TDengine_Resource_fetchFields, ZEND_ACC_PUBLIC) 54 | ZEND_ME(TDengine_Resource, close, arginfo_class_TDengine_Resource_close, ZEND_ACC_PUBLIC) 55 | ZEND_FE_END 56 | }; 57 | 58 | extern PHP_TDENGINE_API zend_class_entry *TDengine_Resource_ce; 59 | extern PHP_TDENGINE_API zend_object_handlers tdengine_resource_handlers; 60 | 61 | inline zend_object *php_tdengine_resource_create_object(zend_class_entry *ce) { 62 | ResourceObject *obj = (ResourceObject *) zend_object_alloc(sizeof(ResourceObject), ce); 63 | obj->ptr = (TDengineResource*) emalloc(sizeof(TDengineResource)); 64 | obj->ptr->res = nullptr; 65 | obj->ptr->connection = nullptr; 66 | obj->ptr->statement = nullptr; 67 | obj->ptr->sql = nullptr; 68 | zend_object *zobj = &obj->std; 69 | zend_object_std_init(zobj, ce); 70 | object_properties_init(zobj, ce); 71 | zobj->handlers = &tdengine_resource_handlers; 72 | return zobj; 73 | } 74 | 75 | inline void php_tdengine_resource_free_object(zend_object *zobj) { 76 | ResourceObject *obj = zend_object_to_object(zobj, ResourceObject); 77 | 78 | if (obj && obj->ptr) 79 | { 80 | TDengineResource *resource = obj->ptr; 81 | if (resource->res) 82 | { 83 | close_resource(resource); 84 | } 85 | if (resource->connection) 86 | { 87 | GC_DELREF(&resource->connection->std); 88 | resource->connection = nullptr; 89 | } 90 | if (resource->statement) 91 | { 92 | GC_DELREF(&resource->statement->std); 93 | resource->statement = nullptr; 94 | } 95 | resource->sql = nullptr; 96 | 97 | efree(resource); 98 | obj->ptr = nullptr; 99 | } 100 | 101 | zend_object_std_dtor(zobj); 102 | } 103 | 104 | inline zend_class_entry *register_class_TDengine_Resource(void) 105 | { 106 | zend_class_entry ce; 107 | 108 | INIT_NS_CLASS_ENTRY(ce, "TDengine", "Resource", class_TDengine_Resource_methods); 109 | TDengine_Resource_ce = zend_register_internal_class_ex(&ce, nullptr); 110 | TDengine_Resource_ce->create_object = php_tdengine_resource_create_object; 111 | memcpy(&tdengine_resource_handlers, &std_object_handlers, sizeof(tdengine_resource_handlers)); 112 | tdengine_resource_handlers.offset = XtOffsetOf(ResourceObject, std); 113 | tdengine_resource_handlers.free_obj = php_tdengine_resource_free_object; 114 | 115 | return TDengine_Resource_ce; 116 | } 117 | 118 | #endif /* PHP_EXT_TAOS_RESOURCE_H */ -------------------------------------------------------------------------------- /include/ext_taos_statement.h: -------------------------------------------------------------------------------- 1 | #pragma once 2 | 3 | #include "ext_tdengine.h" 4 | #include "ext_taos_connection.h" 5 | 6 | #define check_stmt(stmt) \ 7 | if (!assert_stmt(stmt)) \ 8 | { \ 9 | zend_throw_exception_ex(TDengine_Exception_ce, 0, "Invalid stmt"); \ 10 | RETURN_THROWS(); \ 11 | } 12 | 13 | typedef struct { 14 | TAOS_STMT *stmt; 15 | ConnectionObject *connection; 16 | const char *sql; 17 | } TDengineStatement; 18 | 19 | typedef struct { 20 | TDengineStatement *ptr; 21 | zend_object std; 22 | } StatementObject; 23 | 24 | inline int assert_stmt(TDengineStatement *stmt) 25 | { 26 | return stmt->stmt ? 1 : 0; 27 | } 28 | 29 | inline void close_statement(TDengineStatement *stmt) 30 | { 31 | taos_stmt_close(stmt->stmt); 32 | stmt->stmt = nullptr; 33 | } 34 | 35 | // TDengine\Statement 36 | ZEND_METHOD(TDengine_Statement, getConnection); 37 | ZEND_METHOD(TDengine_Statement, getSql); 38 | ZEND_METHOD(TDengine_Statement, close); 39 | ZEND_METHOD(TDengine_Statement, bindParams); 40 | ZEND_METHOD(TDengine_Statement, execute); 41 | ZEND_METHOD(TDengine_Statement, setTableNameTags); 42 | 43 | const zend_function_entry class_TDengine_Statement_methods[] = { 44 | ZEND_ME(TDengine_Statement, getConnection, arginfo_class_TDengine_Statement_getConnection, ZEND_ACC_PUBLIC) 45 | ZEND_ME(TDengine_Statement, getSql, arginfo_class_TDengine_Statement_getSql, ZEND_ACC_PUBLIC) 46 | ZEND_ME(TDengine_Statement, close, arginfo_class_TDengine_Statement_close, ZEND_ACC_PUBLIC) 47 | ZEND_ME(TDengine_Statement, bindParams, arginfo_class_TDengine_Statement_bindParams, ZEND_ACC_PUBLIC) 48 | ZEND_ME(TDengine_Statement, execute, arginfo_class_TDengine_Statement_execute, ZEND_ACC_PUBLIC) 49 | ZEND_ME(TDengine_Statement, setTableNameTags, arginfo_class_TDengine_Statement_setTableNameTags, ZEND_ACC_PUBLIC) 50 | ZEND_FE_END 51 | }; 52 | 53 | extern PHP_TDENGINE_API zend_class_entry *TDengine_Statement_ce; 54 | extern PHP_TDENGINE_API zend_object_handlers tdengine_statement_handlers; 55 | 56 | inline zend_object *php_tdengine_statement_create_object(zend_class_entry *ce) { 57 | StatementObject *obj = (StatementObject *) zend_object_alloc(sizeof(StatementObject), ce); 58 | obj->ptr = (TDengineStatement*) emalloc(sizeof(TDengineStatement)); 59 | obj->ptr->stmt = nullptr; 60 | obj->ptr->connection = nullptr; 61 | obj->ptr->sql = nullptr; 62 | zend_object *zobj = &obj->std; 63 | zend_object_std_init(zobj, ce); 64 | object_properties_init(zobj, ce); 65 | zobj->handlers = &tdengine_statement_handlers; 66 | return zobj; 67 | } 68 | 69 | inline void php_tdengine_statement_free_object(zend_object *zobj) { 70 | StatementObject *obj = zend_object_to_object(zobj, StatementObject); 71 | 72 | if (obj && obj->ptr) 73 | { 74 | TDengineStatement *statement = obj->ptr; 75 | if (statement->stmt) 76 | { 77 | close_statement(statement); 78 | } 79 | if (statement->connection) 80 | { 81 | GC_DELREF(&statement->connection->std); 82 | statement->connection = nullptr; 83 | } 84 | obj->ptr->sql = nullptr; 85 | 86 | efree(statement); 87 | obj->ptr = nullptr; 88 | } 89 | 90 | zend_object_std_dtor(zobj); 91 | } 92 | 93 | inline zend_class_entry *register_class_TDengine_Statement(void) 94 | { 95 | zend_class_entry ce; 96 | 97 | INIT_NS_CLASS_ENTRY(ce, "TDengine", "Statement", class_TDengine_Statement_methods); 98 | TDengine_Statement_ce = zend_register_internal_class_ex(&ce, nullptr); 99 | TDengine_Statement_ce->create_object = php_tdengine_statement_create_object; 100 | memcpy(&tdengine_statement_handlers, &std_object_handlers, sizeof(tdengine_statement_handlers)); 101 | tdengine_statement_handlers.offset = XtOffsetOf(StatementObject, std); 102 | tdengine_statement_handlers.free_obj = php_tdengine_statement_free_object; 103 | 104 | return TDengine_Statement_ce; 105 | } -------------------------------------------------------------------------------- /include/ext_tdengine.h: -------------------------------------------------------------------------------- 1 | #ifndef PHP_EXT_TDENGINE_H 2 | # define PHP_EXT_TDENGINE_H 3 | 4 | #ifdef HAVE_CONFIG_H 5 | # include "config.h" 6 | #endif 7 | #include 8 | #include 9 | #include 10 | #include 11 | 12 | #ifdef __cplusplus 13 | extern "C" { 14 | #endif 15 | 16 | #include "php.h" 17 | #include "Zend/zend_exceptions.h" 18 | #include "ext/spl/spl_exceptions.h" 19 | 20 | #ifdef __cplusplus 21 | } 22 | #endif 23 | 24 | #if defined(WIN32) || defined(_WIN32) || defined(__WIN32__) || defined(WIN64) || defined(_WIN64) || defined(__WIN64__) || defined(__NT__) 25 | #define IS_WIN 1 26 | #else 27 | #define IS_WIN 0 28 | #endif 29 | 30 | #if IS_WIN 31 | # ifdef PHP_TDENGINE_EXPORTS 32 | # define PHP_TDENGINE_API __declspec(dllexport) 33 | # elif defined(COMPILE_DL_TDENGINE) 34 | # define PHP_TDENGINE_API __declspec(dllimport) 35 | # else 36 | # define PHP_TDENGINE_API /* nothing special */ 37 | # endif 38 | #elif defined(__GNUC__) && __GNUC__ >= 4 39 | # define PHP_TDENGINE_API __attribute__ ((visibility("default"))) 40 | #else 41 | # define PHP_TDENGINE_API 42 | #endif 43 | 44 | // PHP < 8.0 45 | #ifndef RETURN_THROWS 46 | #define RETURN_THROWS() RETURN_FALSE 47 | #endif 48 | 49 | #ifndef ZEND_ARG_TYPE_INFO_WITH_DEFAULT_VALUE 50 | #define ZEND_ARG_TYPE_INFO_WITH_DEFAULT_VALUE(pass_by_ref, name, type_hint, allow_null, default_value) ZEND_ARG_TYPE_INFO(pass_by_ref, name, type_hint, allow_null) 51 | #endif 52 | 53 | BEGIN_EXTERN_C() 54 | #include "tdengine_arginfo.h" 55 | END_EXTERN_C() 56 | #include 57 | #include 58 | #ifdef HAVE_SWOOLE 59 | #include "tdengine_swoole.h" 60 | #endif 61 | 62 | #if !HAVE_TAOS_BIND 63 | # define TAOS_BIND TAOS_MULTI_BIND 64 | #endif 65 | 66 | using namespace std; 67 | 68 | #ifdef NO_TSTRERROR 69 | inline const char* inner_tstrerror(int32_t err) 70 | { 71 | return std::to_string(err).c_str(); 72 | } 73 | #endif 74 | 75 | #define this_object(class) zend_object_to_object_ptr(Z_OBJ_P(ZEND_THIS), class) 76 | 77 | #define this_object_container(class) zend_object_to_object(Z_OBJ_P(ZEND_THIS), class) 78 | 79 | #define zend_object_to_object(zobj, class) ((class *)((char *)(zobj) - XtOffsetOf(class, std))) 80 | 81 | #define zend_object_to_object_ptr(zobj, class) (zend_object_to_object(zobj, class))->ptr 82 | 83 | #define object_to_zend_object(obj) ((zend_object*)(obj + 1) - 1) 84 | 85 | #endif /* PHP_EXT_TDENGINE_H */ -------------------------------------------------------------------------------- /include/tdengine_swoole.h: -------------------------------------------------------------------------------- 1 | #pragma once 2 | 3 | #ifdef HAVE_SWOOLE 4 | 5 | #include "swoole_coroutine.h" 6 | using swoole::Coroutine; 7 | 8 | #endif 9 | -------------------------------------------------------------------------------- /make.ps1: -------------------------------------------------------------------------------- 1 | cd (Split-Path -Parent $MyInvocation.MyCommand.Definition) 2 | 3 | phpize 4 | 5 | ./configure 6 | 7 | cmake . 8 | 9 | make -j 10 | -------------------------------------------------------------------------------- /make.sh: -------------------------------------------------------------------------------- 1 | #!/bin/sh -e 2 | __DIR__=$(cd "$(dirname "$0")";pwd) 3 | 4 | COMPILE_PARAMS="--enable-swoole" 5 | 6 | if [ "$(uname | grep -i darwin)"x != ""x ]; then 7 | CPU_COUNT="$(sysctl -n machdep.cpu.core_count)" 8 | else 9 | CPU_COUNT="$(/usr/bin/nproc)" 10 | fi 11 | if [ -z ${CPU_COUNT} ]; then 12 | CPU_COUNT=4 13 | fi 14 | 15 | cd "${__DIR__}" 16 | 17 | if [ "$1" = "cmake" ] ;then 18 | phpize 19 | ./configure ${COMPILE_PARAMS} 20 | cmake . 21 | make -j ${CPU_COUNT} 22 | exit 0 23 | fi 24 | 25 | if [ "$1" = "clean" ] ;then 26 | make clean 27 | phpize --clean 28 | exit 0 29 | fi 30 | 31 | if [ "$1" = "install-module" ] ;then 32 | make tdengine 33 | __EXT_DIR__=$(php-config --extension-dir) 34 | cp lib/tdengine.so "${__EXT_DIR__}" 35 | echo "cp lib/tdengine.so ${__EXT_DIR__}" 36 | exit 0 37 | fi 38 | 39 | if [ "$1" = "help" ] ;then 40 | echo "./make.sh cmake" 41 | echo "./make.sh install-module" 42 | echo "./make.sh clean" 43 | echo "./make.sh debug" 44 | echo "./make.sh trace" 45 | echo "./make.sh library [dev]" 46 | echo "./make.sh" 47 | exit 0 48 | fi 49 | 50 | phpize 51 | if [ "$1" = "debug" ] ;then 52 | ./configure ${COMPILE_PARAMS} 53 | elif [ "$1" = "trace" ] ;then 54 | ./configure ${COMPILE_PARAMS} 55 | else 56 | ./configure ${COMPILE_PARAMS} 57 | fi 58 | make clean 59 | make -j ${CPU_COUNT} 60 | 61 | if [ "$(whoami)" = "root" ]; then 62 | make install 63 | else 64 | sudo make install 65 | fi 66 | -------------------------------------------------------------------------------- /php_tdengine.h: -------------------------------------------------------------------------------- 1 | /* tdengine extension for PHP */ 2 | 3 | #ifndef PHP_TDENGINE_H 4 | # define PHP_TDENGINE_H 5 | 6 | extern zend_module_entry tdengine_module_entry; 7 | # define phpext_tdengine_ptr &tdengine_module_entry 8 | 9 | # define PHP_TDENGINE_VERSION "1.0.5" 10 | 11 | # if defined(ZTS) && defined(COMPILE_DL_TDENGINE) 12 | ZEND_TSRMLS_CACHE_EXTERN() 13 | # endif 14 | 15 | #endif /* PHP_TDENGINE_H */ 16 | -------------------------------------------------------------------------------- /run-tests.ps1: -------------------------------------------------------------------------------- 1 | php run-tests.php --show-diff -q $args 2 | -------------------------------------------------------------------------------- /run-tests.sh: -------------------------------------------------------------------------------- 1 | #!/bin/bash 2 | php run-tests.php --show-diff -q $@ 3 | -------------------------------------------------------------------------------- /src/ext_taos.cc: -------------------------------------------------------------------------------- 1 | #include "ext_tdengine.h" 2 | #include "ext_taos.h" 3 | 4 | /* {{{ void setOptions() */ 5 | PHP_FUNCTION(setOptions) 6 | { 7 | zval *options; 8 | if (zend_parse_parameters(ZEND_NUM_ARGS(), "a", &options) == FAILURE) { 9 | RETURN_THROWS(); 10 | } 11 | zval *hashData; 12 | zend_string *hashKey, *str; 13 | 14 | ZEND_HASH_FOREACH_STR_KEY_VAL_IND(Z_ARRVAL_P(options), hashKey, hashData) { 15 | str = zval_get_string(hashData); 16 | if (0 != taos_options((TSDB_OPTION)(zend_long)hashKey, ZSTR_VAL(str))) 17 | { 18 | zend_string_release(str); 19 | RETURN_FALSE; 20 | } 21 | zend_string_release(str); 22 | } ZEND_HASH_FOREACH_END(); 23 | RETURN_TRUE; 24 | } 25 | /* }}} */ 26 | 27 | /* {{{ void getClientInfo() */ 28 | PHP_FUNCTION(getClientInfo) 29 | { 30 | ZEND_PARSE_PARAMETERS_NONE(); 31 | RETURN_STRING(taos_get_client_info()); 32 | } 33 | /* }}} */ 34 | -------------------------------------------------------------------------------- /src/ext_taos_connection.cc: -------------------------------------------------------------------------------- 1 | #include "ext_taos_connection.h" 2 | #include "ext_taos_resource.h" 3 | 4 | PHP_TDENGINE_API zend_class_entry *TDengine_Connection_ce; 5 | PHP_TDENGINE_API zend_object_handlers tdengine_connection_handlers; 6 | PHP_TDENGINE_API zend_class_entry *TDengine_Exception_ce; 7 | bool taos_inited = false; 8 | 9 | PHP_METHOD(TDengine_Connection, __construct) { 10 | TDengineConnection *connection = this_object(ConnectionObject); 11 | size_t host_len, user_len, pass_len, db_len; 12 | if (zend_parse_parameters(ZEND_NUM_ARGS(), "|slsss!", &connection->host, &host_len, &connection->port, &connection->user, &user_len, &connection->pass, &pass_len, &connection->db, &db_len) == FAILURE) { 13 | RETURN_THROWS(); 14 | } 15 | if (nullptr == connection->host) 16 | { 17 | connection->host = const_cast("127.0.0.1"); 18 | } 19 | if (nullptr == connection->user) 20 | { 21 | connection->user = const_cast("root"); 22 | } 23 | if (nullptr == connection->pass) 24 | { 25 | connection->pass = const_cast("taosdata"); 26 | } 27 | RETURN_TRUE; 28 | } 29 | 30 | PHP_METHOD(TDengine_Connection, connect) { 31 | TDengineConnection *connection = this_object(ConnectionObject); 32 | #ifdef HAVE_SWOOLE 33 | if (Coroutine::get_current()) 34 | { 35 | int error = TSDB_CODE_SUCCESS; 36 | swoole::coroutine::async([&]() { 37 | connection->connection = taos_connect(connection->host, connection->user, connection->pass, connection->db, connection->port); 38 | if (!connection->connection) 39 | { 40 | error = taos_errno(nullptr); 41 | } 42 | }); 43 | if (TSDB_CODE_SUCCESS != error) 44 | { 45 | throw_taos_exception_by_errno(error); 46 | } 47 | } 48 | else 49 | { 50 | #endif 51 | connection->connection = taos_connect(connection->host, connection->user, connection->pass, connection->db, connection->port); 52 | if (nullptr == connection->connection) 53 | { 54 | throw_taos_exception_by_connection(connection); 55 | } 56 | #ifdef HAVE_SWOOLE 57 | } 58 | #endif 59 | } 60 | 61 | PHP_METHOD(TDengine_Connection, close) { 62 | TDengineConnection *connection = this_object(ConnectionObject); 63 | check_connected(connection); 64 | connection_close(connection); 65 | } 66 | 67 | PHP_METHOD(TDengine_Connection, getHost) { 68 | TDengineConnection *connection = this_object(ConnectionObject); 69 | RETURN_STRING(connection->host); 70 | } 71 | 72 | PHP_METHOD(TDengine_Connection, getPort) { 73 | TDengineConnection *connection = this_object(ConnectionObject); 74 | RETURN_LONG(connection->port); 75 | } 76 | 77 | PHP_METHOD(TDengine_Connection, getUser) { 78 | TDengineConnection *connection = this_object(ConnectionObject); 79 | RETURN_STRING(connection->user); 80 | } 81 | 82 | PHP_METHOD(TDengine_Connection, getPass) { 83 | TDengineConnection *connection = this_object(ConnectionObject); 84 | RETURN_STRING(connection->pass); 85 | } 86 | 87 | PHP_METHOD(TDengine_Connection, getDb) { 88 | TDengineConnection *connection = this_object(ConnectionObject); 89 | if (connection->db) 90 | { 91 | RETURN_STRING(connection->db); 92 | } 93 | else 94 | { 95 | RETURN_NULL(); 96 | } 97 | } 98 | 99 | PHP_METHOD(TDengine_Connection, getServerInfo) { 100 | TDengineConnection *connection = this_object(ConnectionObject); 101 | check_connected(connection); 102 | const char *result = nullptr; 103 | #ifdef HAVE_SWOOLE 104 | if (Coroutine::get_current()) 105 | { 106 | int error = TSDB_CODE_SUCCESS; 107 | swoole::coroutine::async([&]() { 108 | result = taos_get_server_info(connection->connection); 109 | if (result) 110 | { 111 | RETURN_STRING(result); 112 | } 113 | else 114 | { 115 | error = taos_errno(nullptr); 116 | } 117 | if (TSDB_CODE_SUCCESS != error) 118 | { 119 | throw_taos_exception_by_errno(error); 120 | } 121 | }); 122 | } 123 | else 124 | { 125 | #endif 126 | result = taos_get_server_info(connection->connection); 127 | if (result) 128 | { 129 | RETURN_STRING(result); 130 | } 131 | throw_taos_exception_by_connection(connection); 132 | #ifdef HAVE_SWOOLE 133 | } 134 | #endif 135 | } 136 | 137 | PHP_METHOD(TDengine_Connection, selectDb) { 138 | char *db; 139 | size_t db_len; 140 | if (zend_parse_parameters(ZEND_NUM_ARGS(), "s", &db, &db_len) == FAILURE) { 141 | RETURN_THROWS(); 142 | } 143 | TDengineConnection *connection = this_object(ConnectionObject); 144 | check_connected(connection); 145 | 146 | int ret; 147 | #ifdef HAVE_SWOOLE 148 | if (Coroutine::get_current()) 149 | { 150 | swoole::coroutine::async([&]() { 151 | ret = taos_select_db(connection->connection, db); 152 | }); 153 | } 154 | else 155 | { 156 | #endif 157 | ret = taos_select_db(connection->connection, db); 158 | #ifdef HAVE_SWOOLE 159 | } 160 | #endif 161 | 162 | if(TSDB_CODE_SUCCESS != ret) 163 | { 164 | throw_taos_exception_by_errno(ret); 165 | } 166 | } 167 | 168 | PHP_METHOD(TDengine_Connection, query) { 169 | char *sql; 170 | size_t sql_len; 171 | if (zend_parse_parameters(ZEND_NUM_ARGS(), "s", &sql, &sql_len) == FAILURE) { 172 | RETURN_THROWS(); 173 | } 174 | TDengineConnection *connection = this_object(ConnectionObject); 175 | check_connected(connection); 176 | 177 | TAOS_RES *res = nullptr; 178 | int error; 179 | #ifdef HAVE_SWOOLE 180 | if (Coroutine::get_current()) 181 | { 182 | swoole::coroutine::async([&]() { 183 | res = taos_query(connection->connection, sql); 184 | error = taos_errno(res); 185 | if (TSDB_CODE_SUCCESS != error && res) 186 | { 187 | taos_free_result(res); 188 | } 189 | }); 190 | if (TSDB_CODE_SUCCESS != error) 191 | { 192 | throw_taos_exception_by_errno(error); 193 | } 194 | } 195 | else 196 | { 197 | #endif 198 | res = taos_query(connection->connection, sql); 199 | error = taos_errno(res); 200 | if (TSDB_CODE_SUCCESS != error) 201 | { 202 | if (res) 203 | { 204 | taos_free_result(res); 205 | } 206 | throw_taos_exception_by_errno(error); 207 | } 208 | #ifdef HAVE_SWOOLE 209 | } 210 | #endif 211 | 212 | object_init_ex(return_value, TDengine_Resource_ce); 213 | TDengineResource *resource = zend_object_to_object_ptr(Z_OBJ_P(return_value), ResourceObject); 214 | resource->res = res; 215 | resource->connection = this_object_container(ConnectionObject); 216 | resource->sql = sql; 217 | GC_ADDREF(Z_OBJ_P(ZEND_THIS)); 218 | } 219 | 220 | PHP_METHOD(TDengine_Connection, prepare) { 221 | char *sql; 222 | unsigned long sql_len; 223 | if (zend_parse_parameters(ZEND_NUM_ARGS(), "s", &sql, &sql_len) == FAILURE) { 224 | RETURN_THROWS(); 225 | } 226 | TDengineConnection *connection = this_object(ConnectionObject); 227 | check_connected(connection); 228 | 229 | TAOS_STMT *stmt = taos_stmt_init(connection->connection); 230 | int error; 231 | #ifdef HAVE_SWOOLE 232 | if (Coroutine::get_current()) 233 | { 234 | swoole::coroutine::async([&]() { 235 | error = taos_stmt_prepare(stmt, sql, sql_len); 236 | if (TSDB_CODE_SUCCESS != error) 237 | { 238 | taos_stmt_close(stmt); 239 | } 240 | }); 241 | if (TSDB_CODE_SUCCESS != error) 242 | { 243 | throw_taos_exception_by_errno(error); 244 | } 245 | } 246 | else 247 | { 248 | #endif 249 | error = taos_stmt_prepare(stmt, sql, sql_len); 250 | if (TSDB_CODE_SUCCESS != error) 251 | { 252 | const char* message = taos_stmt_errstr(stmt); 253 | taos_stmt_close(stmt); 254 | throw_taos_exception(message, error); 255 | } 256 | #ifdef HAVE_SWOOLE 257 | } 258 | #endif 259 | object_init_ex(return_value, TDengine_Statement_ce); 260 | TDengineStatement *statement = zend_object_to_object_ptr(Z_OBJ_P(return_value), StatementObject); 261 | statement->stmt = stmt; 262 | statement->connection = this_object_container(ConnectionObject); 263 | statement->sql = sql; 264 | GC_ADDREF(Z_OBJ_P(ZEND_THIS)); 265 | } 266 | 267 | PHP_METHOD(TDengine_Connection, isConnected) { 268 | TDengineConnection *connection = this_object(ConnectionObject); 269 | RETURN_BOOL(is_connected(connection)); 270 | } 271 | -------------------------------------------------------------------------------- /src/ext_taos_resource.cc: -------------------------------------------------------------------------------- 1 | #include "ext_tdengine.h" 2 | #include "ext_taos_resource.h" 3 | 4 | extern "C" { 5 | #include "ext/standard/php_math.h" 6 | } 7 | 8 | PHP_TDENGINE_API zend_class_entry *TDengine_Resource_ce; 9 | PHP_TDENGINE_API zend_object_handlers tdengine_resource_handlers; 10 | 11 | bool fetch_row(zval *zrow, TDengineResource *resource, TAOS_FIELD *fields, int field_count) 12 | { 13 | array_init_size(zrow, field_count); 14 | TAOS_ROW row = nullptr; 15 | #ifdef HAVE_SWOOLE 16 | if (Coroutine::get_current()) 17 | { 18 | swoole::coroutine::async([&]() { 19 | row = taos_fetch_row(resource->res); 20 | }); 21 | } 22 | else 23 | { 24 | #endif 25 | row = taos_fetch_row(resource->res); 26 | #ifdef HAVE_SWOOLE 27 | } 28 | #endif 29 | if (!row) 30 | { 31 | return false; 32 | } 33 | int16_t len; 34 | char *string_value; 35 | 36 | for(int i = 0; i < field_count; ++i) 37 | { 38 | if (nullptr == row[i]) 39 | { 40 | add_assoc_null(zrow, fields[i].name); 41 | continue; 42 | } 43 | switch(fields[i].type) 44 | { 45 | case TSDB_DATA_TYPE_NULL: 46 | add_assoc_null(zrow, fields[i].name); 47 | break; 48 | case TSDB_DATA_TYPE_BOOL: 49 | add_assoc_bool(zrow, fields[i].name, 0 != *((int8_t *)row[i])); 50 | break; 51 | case TSDB_DATA_TYPE_TINYINT: 52 | add_assoc_long(zrow, fields[i].name, *((int8_t *)row[i])); 53 | break; 54 | case TSDB_DATA_TYPE_SMALLINT: 55 | add_assoc_long(zrow, fields[i].name, *((int16_t *)row[i])); 56 | break; 57 | case TSDB_DATA_TYPE_INT: 58 | add_assoc_long(zrow, fields[i].name, *((int32_t *)row[i])); 59 | break; 60 | case TSDB_DATA_TYPE_BIGINT: 61 | add_assoc_long(zrow, fields[i].name, *((int64_t *)row[i])); 62 | break; 63 | case TSDB_DATA_TYPE_FLOAT: 64 | add_assoc_double(zrow, fields[i].name, _php_math_round((double) *((float *)row[i]), 7, PHP_ROUND_HALF_DOWN)); 65 | break; 66 | case TSDB_DATA_TYPE_DOUBLE: 67 | add_assoc_double(zrow, fields[i].name, *((double *)row[i])); 68 | break; 69 | case TSDB_DATA_TYPE_BINARY: 70 | case TSDB_DATA_TYPE_NCHAR: 71 | #ifdef TSDB_DATA_TYPE_JSON 72 | case TSDB_DATA_TYPE_JSON: 73 | #endif 74 | len = ((int16_t *)((char*)row[i] - sizeof(int16_t)))[0]; 75 | string_value = (char *) emalloc(len); 76 | memcpy(string_value, row[i], len); 77 | add_assoc_stringl(zrow, fields[i].name, string_value, len); 78 | efree(string_value); 79 | break; 80 | case TSDB_DATA_TYPE_TIMESTAMP: 81 | add_assoc_long(zrow, fields[i].name, *((int64_t *)row[i])); 82 | break; 83 | #ifdef TSDB_DATA_TYPE_UTINYINT 84 | case TSDB_DATA_TYPE_UTINYINT: 85 | add_assoc_long(zrow, fields[i].name, *((uint8_t *)row[i])); 86 | break; 87 | #endif 88 | #ifdef TSDB_DATA_TYPE_USMALLINT 89 | case TSDB_DATA_TYPE_USMALLINT: 90 | add_assoc_long(zrow, fields[i].name, *((uint16_t *)row[i])); 91 | break; 92 | #endif 93 | #ifdef TSDB_DATA_TYPE_UINT 94 | case TSDB_DATA_TYPE_UINT: 95 | add_assoc_long(zrow, fields[i].name, *((uint32_t *)row[i])); 96 | break; 97 | #endif 98 | #ifdef TSDB_DATA_TYPE_UBIGINT 99 | case TSDB_DATA_TYPE_UBIGINT: 100 | add_assoc_long(zrow, fields[i].name, *((uint64_t *)row[i])); 101 | break; 102 | #endif 103 | default: 104 | zend_throw_exception_ex(TDengine_Exception_ce, 0, "Invalid field type %d", fields[i].type); 105 | return false; 106 | } 107 | } 108 | 109 | return true; 110 | } 111 | 112 | void fetch(zval *zv, TDengineResource *resource) 113 | { 114 | array_init(zv); 115 | int field_count = taos_num_fields(resource->res); 116 | TAOS_FIELD *fields = taos_fetch_fields(resource->res); 117 | while (true) 118 | { 119 | zval zrow; 120 | if (fetch_row(&zrow, resource, fields, field_count)) 121 | { 122 | add_next_index_zval(zv, &zrow); 123 | } 124 | else 125 | { 126 | break; 127 | } 128 | } 129 | } 130 | 131 | void fetch_fields(zval *zresult, TDengineResource *resource) 132 | { 133 | TAOS_FIELD *fields = taos_fetch_fields(resource->res); 134 | int field_count = taos_num_fields(resource->res); 135 | array_init_size(zresult, field_count); 136 | for(int i = 0; i < field_count; ++i) 137 | { 138 | zval zrow; 139 | array_init_size(&zrow, 3); 140 | add_assoc_string(&zrow, "name", fields[i].name); 141 | add_assoc_long(&zrow, "type", fields[i].type); 142 | add_assoc_long(&zrow, "bytes", fields[i].bytes); 143 | add_next_index_zval(zresult, &zrow); 144 | } 145 | } 146 | 147 | PHP_METHOD(TDengine_Resource, getConnection) { 148 | TDengineResource *resource = this_object(ResourceObject); 149 | 150 | GC_ADDREF(&resource->connection->std); 151 | RETURN_OBJ(&resource->connection->std); 152 | } 153 | 154 | PHP_METHOD(TDengine_Resource, getStatement) { 155 | TDengineResource *resource = this_object(ResourceObject); 156 | 157 | if (resource->statement) 158 | { 159 | GC_ADDREF(&resource->statement->std); 160 | RETURN_OBJ(&resource->statement->std); 161 | } 162 | else 163 | { 164 | RETURN_NULL(); 165 | } 166 | } 167 | 168 | PHP_METHOD(TDengine_Resource, getSql) { 169 | TDengineResource *resource = this_object(ResourceObject); 170 | 171 | RETURN_STRING(resource->sql ? resource->sql : ""); 172 | } 173 | 174 | PHP_METHOD(TDengine_Resource, getResultPrecision) { 175 | TDengineResource *resource = this_object(ResourceObject); 176 | 177 | RETURN_LONG(resource->res ? taos_result_precision(resource->res) : 0); 178 | } 179 | 180 | PHP_METHOD(TDengine_Resource, fetch) { 181 | TDengineResource *resource = this_object(ResourceObject); 182 | fetch(return_value, resource); 183 | } 184 | 185 | PHP_METHOD(TDengine_Resource, fetchRow) { 186 | TDengineResource *resource = this_object(ResourceObject); 187 | if (resource->res) 188 | { 189 | fetch_row(return_value, resource, taos_fetch_fields(resource->res), taos_num_fields(resource->res)); 190 | } 191 | else 192 | { 193 | array_init_size(return_value, 0); 194 | } 195 | } 196 | 197 | PHP_METHOD(TDengine_Resource, getFieldCount) { 198 | TDengineResource *resource = this_object(ResourceObject); 199 | RETURN_LONG(resource->res ? taos_num_fields(resource->res) : 0); 200 | } 201 | 202 | PHP_METHOD(TDengine_Resource, affectedRows) { 203 | TDengineResource *resource = this_object(ResourceObject); 204 | if (resource->statement) 205 | { 206 | RETURN_LONG(taos_stmt_affected_rows(resource->statement->ptr->stmt)); 207 | } 208 | RETURN_LONG(resource->res ? taos_affected_rows(resource->res) : 0); 209 | } 210 | 211 | PHP_METHOD(TDengine_Resource, fetchFields) { 212 | TDengineResource *resource = this_object(ResourceObject); 213 | if (resource->res) 214 | { 215 | fetch_fields(return_value, resource); 216 | } 217 | else 218 | { 219 | array_init_size(return_value, 0); 220 | } 221 | } 222 | 223 | PHP_METHOD(TDengine_Resource, close) { 224 | TDengineResource *resource = this_object(ResourceObject); 225 | close_resource(resource); 226 | } 227 | -------------------------------------------------------------------------------- /src/ext_taos_statement.cc: -------------------------------------------------------------------------------- 1 | #include "ext_tdengine.h" 2 | #include "ext_taos_statement.h" 3 | #include "ext_taos_resource.h" 4 | 5 | PHP_TDENGINE_API zend_class_entry *TDengine_Statement_ce; 6 | PHP_TDENGINE_API zend_object_handlers tdengine_statement_handlers; 7 | #if HAVE_TAOS_BIND 8 | static int is_null = 1; 9 | #else 10 | static char is_null = 1; 11 | #endif 12 | 13 | inline bool parse_taos_bind(TAOS_BIND *bind, int data_type, zval *value) 14 | { 15 | bind->is_null = nullptr; 16 | zend_string *str; 17 | switch (data_type) 18 | { 19 | case TSDB_DATA_TYPE_NULL: 20 | bind->is_null = &is_null; 21 | bind->buffer_length = 0; 22 | break; 23 | case TSDB_DATA_TYPE_BOOL: 24 | bind->buffer = (int8_t*) emalloc(sizeof(int8_t)); 25 | *((int8_t*) bind->buffer) = Z_TYPE_P(value) == IS_TRUE ? 1 : 0; 26 | bind->buffer_length = sizeof(int8_t); 27 | break; 28 | case TSDB_DATA_TYPE_TINYINT: 29 | bind->buffer = (int8_t*) emalloc(sizeof(int8_t)); 30 | *((int8_t*) bind->buffer) = (int8_t) zval_get_long(value); 31 | bind->buffer_length = sizeof(int8_t); 32 | break; 33 | case TSDB_DATA_TYPE_SMALLINT: 34 | bind->buffer = (int16_t*) emalloc(sizeof(int16_t)); 35 | *((int16_t*) bind->buffer) = (int16_t) zval_get_long(value); 36 | bind->buffer_length = sizeof(int16_t); 37 | break; 38 | case TSDB_DATA_TYPE_INT: 39 | bind->buffer = (int32_t*) emalloc(sizeof(int32_t)); 40 | *((int32_t*) bind->buffer) = (int32_t) zval_get_long(value); 41 | bind->buffer_length = sizeof(int32_t); 42 | break; 43 | case TSDB_DATA_TYPE_BIGINT: 44 | bind->buffer = (int64_t*) emalloc(sizeof(int64_t)); 45 | *((int64_t*) bind->buffer) = (int64_t) zval_get_long(value); 46 | bind->buffer_length = sizeof(int64_t); 47 | break; 48 | case TSDB_DATA_TYPE_FLOAT: 49 | bind->buffer = (float*) emalloc(sizeof(float)); 50 | *((float*) bind->buffer) = (float) zval_get_double(value); 51 | bind->buffer_length = sizeof(float); 52 | break; 53 | case TSDB_DATA_TYPE_DOUBLE: 54 | bind->buffer = (double*) emalloc(sizeof(double)); 55 | *((double*) bind->buffer) = zval_get_double(value); 56 | bind->buffer_length = sizeof(double); 57 | break; 58 | case TSDB_DATA_TYPE_BINARY: 59 | case TSDB_DATA_TYPE_NCHAR: 60 | #ifdef TSDB_DATA_TYPE_JSON 61 | case TSDB_DATA_TYPE_JSON: 62 | #endif 63 | str = zval_get_string(value); 64 | bind->buffer_length = (int16_t) ZSTR_LEN(str); 65 | bind->buffer = emalloc(bind->buffer_length); 66 | memcpy(bind->buffer, ZSTR_VAL(str), bind->buffer_length); 67 | zend_string_release(str); 68 | break; 69 | case TSDB_DATA_TYPE_TIMESTAMP: 70 | bind->buffer = (int64_t*) emalloc(sizeof(int64_t)); 71 | *((int64_t*) bind->buffer) = zval_get_long(value); 72 | bind->buffer_length = sizeof(int64_t); 73 | break; 74 | #ifdef TSDB_DATA_TYPE_UTINYINT 75 | case TSDB_DATA_TYPE_UTINYINT: 76 | bind->buffer = (uint8_t*) emalloc(sizeof(uint8_t)); 77 | *((uint8_t*) bind->buffer) = (uint8_t) zval_get_long(value); 78 | bind->buffer_length = sizeof(uint8_t); 79 | break; 80 | #endif 81 | #ifdef TSDB_DATA_TYPE_USMALLINT 82 | case TSDB_DATA_TYPE_USMALLINT: 83 | bind->buffer = (uint16_t*) emalloc(sizeof(uint16_t)); 84 | *((uint16_t*) bind->buffer) = (uint16_t) zval_get_long(value); 85 | bind->buffer_length = sizeof(uint16_t); 86 | break; 87 | #endif 88 | #ifdef TSDB_DATA_TYPE_UINT 89 | case TSDB_DATA_TYPE_UINT: 90 | bind->buffer = (uint32_t*) emalloc(sizeof(uint32_t)); 91 | *((uint32_t*) bind->buffer) = (uint32_t) zval_get_long(value); 92 | bind->buffer_length = sizeof(uint32_t); 93 | break; 94 | #endif 95 | #ifdef TSDB_DATA_TYPE_UBIGINT 96 | case TSDB_DATA_TYPE_UBIGINT: 97 | bind->buffer = (uint64_t*) emalloc(sizeof(uint64_t)); 98 | *((uint64_t*) bind->buffer) = (uint64_t) zval_get_long(value); 99 | bind->buffer_length = sizeof(uint64_t); 100 | break; 101 | #endif 102 | default: 103 | zend_throw_exception_ex(TDengine_Exception_ce, 0, "Invalid field type %d", data_type); 104 | return false; 105 | } 106 | bind->buffer_type = data_type; 107 | #if HAVE_TAOS_BIND 108 | bind->length = &bind->buffer_length; 109 | #else 110 | bind->num = 1; 111 | bind->length = (int32_t*) &bind->buffer_length; 112 | #endif 113 | return true; 114 | } 115 | 116 | PHP_METHOD(TDengine_Statement, getConnection) { 117 | TDengineStatement *statement = this_object(StatementObject); 118 | 119 | GC_ADDREF(&statement->connection->std); 120 | RETURN_OBJ(&statement->connection->std); 121 | } 122 | 123 | PHP_METHOD(TDengine_Statement, getSql) { 124 | TDengineStatement *statement = this_object(StatementObject); 125 | 126 | RETURN_STRING(statement->sql ? statement->sql : ""); 127 | } 128 | 129 | PHP_METHOD(TDengine_Statement, close) { 130 | TDengineStatement *statement = this_object(StatementObject); 131 | check_stmt(statement); 132 | close_statement(statement); 133 | } 134 | 135 | PHP_METHOD(TDengine_Statement, bindParams) { 136 | zval *params; 137 | if (zend_parse_parameters(ZEND_NUM_ARGS(), "a", ¶ms) == FAILURE) { 138 | RETURN_THROWS(); 139 | } 140 | HashTable *params_ht = Z_ARRVAL_P(params); 141 | TDengineStatement *statement = this_object(StatementObject); 142 | check_stmt(statement); 143 | 144 | TAOS_BIND *binds = (TAOS_BIND*) ecalloc(zend_array_count(params_ht), sizeof(TAOS_BIND)); 145 | zval *row, *type, *value; 146 | size_t index; 147 | bool success = true; 148 | ZEND_HASH_FOREACH_NUM_KEY_VAL(params_ht, index, row) { 149 | if ( 150 | ((type = zend_hash_str_find(Z_ARRVAL_P(row), ZEND_STRL("type"))) && (value = zend_hash_str_find(Z_ARRVAL_P(row), ZEND_STRL("value")))) 151 | || ((type = zend_hash_index_find(Z_ARRVAL_P(row), 0)) && (value = zend_hash_index_find(Z_ARRVAL_P(row), 1))) 152 | ) 153 | { 154 | if (!parse_taos_bind(&binds[index], (int) zval_get_long(type), value)) 155 | { 156 | success = false; 157 | break; 158 | } 159 | } 160 | else 161 | { 162 | success = false; 163 | zend_throw_exception(spl_ce_InvalidArgumentException, "Invalid bind params", 0); 164 | break; 165 | } 166 | } ZEND_HASH_FOREACH_END(); 167 | 168 | if (success) 169 | { 170 | int error = taos_stmt_bind_param(statement->stmt, binds); 171 | efree(binds); 172 | if (TSDB_CODE_SUCCESS != error) 173 | { 174 | throw_taos_exception_by_errno(error); 175 | } 176 | int is_insert; 177 | error = taos_stmt_is_insert(statement->stmt, &is_insert); 178 | if (TSDB_CODE_SUCCESS == error) 179 | { 180 | if (is_insert) 181 | { 182 | error = taos_stmt_add_batch(statement->stmt); 183 | if (TSDB_CODE_SUCCESS != error) 184 | { 185 | throw_taos_exception_by_errno(error); 186 | } 187 | } 188 | } 189 | else 190 | { 191 | throw_taos_exception_by_errno(error); 192 | } 193 | } 194 | else 195 | { 196 | efree(binds); 197 | } 198 | } 199 | 200 | PHP_METHOD(TDengine_Statement, execute) { 201 | TDengineStatement *statement = this_object(StatementObject); 202 | check_stmt(statement); 203 | 204 | int error = TSDB_CODE_SUCCESS; 205 | #ifdef HAVE_SWOOLE 206 | if (Coroutine::get_current()) 207 | { 208 | swoole::coroutine::async([&]() { 209 | error = taos_stmt_execute(statement->stmt); 210 | }); 211 | } 212 | else 213 | { 214 | #endif 215 | error = taos_stmt_execute(statement->stmt); 216 | #ifdef HAVE_SWOOLE 217 | } 218 | #endif 219 | 220 | if (TSDB_CODE_SUCCESS != error) 221 | { 222 | throw_taos_exception_by_errno(error); 223 | } 224 | 225 | TAOS_RES *res = taos_stmt_use_result(statement->stmt); 226 | 227 | object_init_ex(return_value, TDengine_Resource_ce); 228 | TDengineResource *resource = zend_object_to_object_ptr(Z_OBJ_P(return_value), ResourceObject); 229 | resource->res = res; 230 | resource->connection = statement->connection; 231 | resource->sql = statement->sql; 232 | resource->statement = this_object_container(StatementObject); 233 | GC_ADDREF(&statement->connection->std); 234 | GC_ADDREF(Z_OBJ_P(ZEND_THIS)); 235 | } 236 | 237 | PHP_METHOD(TDengine_Statement, setTableNameTags) { 238 | char *table_name; 239 | size_t table_name_len; 240 | zval *tags; 241 | if (zend_parse_parameters(ZEND_NUM_ARGS(), "s|a", &table_name, &table_name_len, &tags) == FAILURE) { 242 | RETURN_THROWS(); 243 | } 244 | HashTable *tags_ht = Z_ARRVAL_P(tags); 245 | TDengineStatement *statement = this_object(StatementObject); 246 | check_stmt(statement); 247 | 248 | TAOS_BIND *binds = (TAOS_BIND*) ecalloc(zend_array_count(tags_ht), sizeof(TAOS_BIND)); 249 | zval *row, *type, *value; 250 | size_t index; 251 | bool success = true; 252 | ZEND_HASH_FOREACH_NUM_KEY_VAL(tags_ht, index, row) { 253 | if ( 254 | ((type = zend_hash_str_find(Z_ARRVAL_P(row), ZEND_STRL("type"))) && (value = zend_hash_str_find(Z_ARRVAL_P(row), ZEND_STRL("value")))) 255 | || ((type = zend_hash_index_find(Z_ARRVAL_P(row), 0)) && (value = zend_hash_index_find(Z_ARRVAL_P(row), 1))) 256 | ) 257 | { 258 | if (!parse_taos_bind(&binds[index], (int) zval_get_long(type), value)) 259 | { 260 | success = false; 261 | break; 262 | } 263 | } 264 | else 265 | { 266 | success = false; 267 | zend_throw_exception(spl_ce_InvalidArgumentException, "Invalid bind params", 0); 268 | break; 269 | } 270 | } ZEND_HASH_FOREACH_END(); 271 | 272 | if (success) 273 | { 274 | int error = taos_stmt_set_tbname_tags(statement->stmt, table_name, binds); 275 | efree(binds); 276 | if (TSDB_CODE_SUCCESS != error) 277 | { 278 | throw_taos_exception_by_errno(error); 279 | } 280 | } 281 | else 282 | { 283 | efree(binds); 284 | } 285 | } 286 | -------------------------------------------------------------------------------- /tdengine.cc: -------------------------------------------------------------------------------- 1 | /* tdengine extension for PHP */ 2 | 3 | #ifdef HAVE_CONFIG_H 4 | # include "config.h" 5 | #endif 6 | 7 | #include "php.h" 8 | #include "ext/standard/info.h" 9 | #include "Zend/zend_exceptions.h" 10 | #include 11 | #include "php_tdengine.h" 12 | #include "ext_taos.h" 13 | #include "ext_taos_connection.h" 14 | #include "ext_taos_resource.h" 15 | #include "ext_taos_statement.h" 16 | 17 | /* For compatibility with older PHP versions */ 18 | #ifndef ZEND_PARSE_PARAMETERS_NONE 19 | #define ZEND_PARSE_PARAMETERS_NONE() \ 20 | ZEND_PARSE_PARAMETERS_START(0, 0) \ 21 | ZEND_PARSE_PARAMETERS_END() 22 | #endif 23 | 24 | /* {{{ PHP_MINIT_FUNCTION */ 25 | PHP_MINIT_FUNCTION(tdengine) 26 | { 27 | register_constants(module_number); 28 | register_class_TDengine_Connection(); 29 | register_class_TDengine_Resource(); 30 | register_class_TDengine_Statement(); 31 | register_class_TDengine_Exception(zend_ce_exception); 32 | return SUCCESS; 33 | } 34 | /* }}} */ 35 | 36 | /* {{{ PHP_MSHUTDOWN_FUNCTION */ 37 | PHP_MSHUTDOWN_FUNCTION(tdengine) 38 | { 39 | if (taos_inited) 40 | { 41 | taos_cleanup(); 42 | taos_inited = false; 43 | } 44 | return SUCCESS; 45 | } 46 | /* }}} */ 47 | 48 | /* {{{ PHP_RINIT_FUNCTION */ 49 | PHP_RINIT_FUNCTION(tdengine) 50 | { 51 | #if defined(ZTS) && defined(COMPILE_DL_TDENGINE) 52 | ZEND_TSRMLS_CACHE_UPDATE(); 53 | #endif 54 | return SUCCESS; 55 | } 56 | /* }}} */ 57 | 58 | /* {{{ PHP_MINFO_FUNCTION */ 59 | PHP_MINFO_FUNCTION(tdengine) 60 | { 61 | php_info_print_table_start(); 62 | php_info_print_table_row(2, "version", PHP_TDENGINE_VERSION); 63 | php_info_print_table_header(2, "tdengine support", "enabled"); 64 | php_info_print_table_row(2, "tdengine", taos_get_client_info()); 65 | #ifdef HAVE_SWOOLE 66 | php_info_print_table_row(2, "swoole", SWOOLE_VERSION); 67 | #endif 68 | php_info_print_table_end(); 69 | } 70 | /* }}} */ 71 | 72 | /* {{{ tdengine_module_entry */ 73 | zend_module_entry tdengine_module_entry = { 74 | STANDARD_MODULE_HEADER, 75 | "tdengine", /* Extension name */ 76 | ext_functions, /* zend_function_entry */ 77 | PHP_MINIT(tdengine), /* PHP_MINIT - Module initialization */ 78 | PHP_MSHUTDOWN(tdengine), /* PHP_MSHUTDOWN - Module shutdown */ 79 | PHP_RINIT(tdengine), /* PHP_RINIT - Request initialization */ 80 | nullptr, /* PHP_RSHUTDOWN - Request shutdown */ 81 | PHP_MINFO(tdengine), /* PHP_MINFO - Module info */ 82 | PHP_TDENGINE_VERSION, /* Version */ 83 | STANDARD_MODULE_PROPERTIES 84 | }; 85 | /* }}} */ 86 | 87 | #ifdef COMPILE_DL_TDENGINE 88 | # ifdef ZTS 89 | ZEND_TSRMLS_CACHE_DEFINE() 90 | # endif 91 | ZEND_GET_MODULE(tdengine) 92 | #endif 93 | -------------------------------------------------------------------------------- /tdengine.stub.php: -------------------------------------------------------------------------------- 1 | 9 | --EXPECT-- 10 | The extension "tdengine" is available 11 | -------------------------------------------------------------------------------- /tests/01-sync/002-connect-close.phpt: -------------------------------------------------------------------------------- 1 | --TEST-- 2 | test connect and close 3 | --EXTENSIONS-- 4 | tdengine 5 | --FILE-- 6 | getHost()); 18 | assert(getPort() === $connection->getPort()); 19 | assert(getUser() === $connection->getUser()); 20 | assert(getPass() === $connection->getPass()); 21 | assert(getDb() === $connection->getDb()); 22 | $connection->connect(); 23 | assert(true === $connection->isConnected()); 24 | $connection->close(); 25 | ?> 26 | --EXPECT-- 27 | -------------------------------------------------------------------------------- /tests/01-sync/003-query.phpt: -------------------------------------------------------------------------------- 1 | --TEST-- 2 | test query 3 | --EXTENSIONS-- 4 | tdengine 5 | --FILE-- 6 | connect(); 15 | 16 | $sql = 'create database if not exists php_tdengine_test'; 17 | $resource = $connection->query($sql); 18 | Assert::eq($resource->getSql(), $sql); 19 | 20 | $connection->selectDb('php_tdengine_test'); 21 | 22 | $sql = 'create table if not exists test_query (ts timestamp, temperature int, humidity float)'; 23 | $resource = $connection->query($sql); 24 | Assert::eq($resource->getSql(), $sql); 25 | 26 | $time = (int) (microtime(true) * 1000); 27 | $sql = sprintf('insert into test_query values(%s,%s,%s)', $time, 36, 44.0); 28 | $resource = $connection->query($sql); 29 | Assert::eq($resource->affectedRows(), 1); 30 | Assert::eq($resource->getSql(), $sql); 31 | 32 | $sql = 'select * from test_query order by ts desc limit 1'; 33 | $resource = $connection->query($sql); 34 | Assert::eq($resource->getSql(), $sql); 35 | Assert::eq($resource->fetch(), [ 36 | [ 37 | 'ts' => $time, 38 | 'temperature' => 36, 39 | 'humidity' => 44.0, 40 | ], 41 | ]); 42 | 43 | $resource = $connection->query($sql); 44 | Assert::eq($resource->getSql(), $sql); 45 | Assert::eq($resource->fetchRow(), [ 46 | 'ts' => $time, 47 | 'temperature' => 36, 48 | 'humidity' => 44.0, 49 | ]); 50 | 51 | // bug: https://github.com/Yurunsoft/php-tdengine/issues/6 52 | $sql = <<<'SQL' 53 | SELECT 54 | AVG(temperature) as avg_temperature 55 | FROM 56 | test_query 57 | WHERE 58 | ts >= NOW - 1d 59 | and ts <= now INTERVAL(10s) FILL(PREV) 60 | limit 61 | 1; 62 | SQL; 63 | $resource = $connection->query($sql); 64 | Assert::eq($resource->getSql(), $sql); 65 | $row = $resource->fetchRow(); 66 | Assert::true(array_key_exists('avg_temperature', $row)); 67 | Assert::null($row['avg_temperature']); 68 | ?> 69 | --EXPECT-- 70 | -------------------------------------------------------------------------------- /tests/01-sync/004-prepare.phpt: -------------------------------------------------------------------------------- 1 | --TEST-- 2 | test prepare 3 | --EXTENSIONS-- 4 | tdengine 5 | --FILE-- 6 | connect(); 15 | 16 | $sql = 'create database if not exists php_tdengine_test'; 17 | $resource = $connection->query($sql); 18 | Assert::eq($resource->getSql(), $sql); 19 | 20 | $connection->selectDb('php_tdengine_test'); 21 | 22 | $sql = 'create table if not exists test_query (ts timestamp, temperature int, humidity float)'; 23 | $resource = $connection->query($sql); 24 | Assert::eq($resource->getSql(), $sql); 25 | 26 | $time1 = (int) (microtime(true) * 1000); 27 | $sql = 'insert into test_query values(?,?,?)'; 28 | $stmt = $connection->prepare($sql); 29 | $stmt->bindParams([ 30 | [TDengine\TSDB_DATA_TYPE_TIMESTAMP, $time1], 31 | [TDengine\TSDB_DATA_TYPE_INT, 36], 32 | [TDengine\TSDB_DATA_TYPE_FLOAT, 44.0], 33 | ]); 34 | $resource = $stmt->execute(); 35 | Assert::eq($resource->affectedRows(), 1); 36 | Assert::eq($resource->getSql(), $sql); 37 | 38 | $time2 = (int) (microtime(true) * 1000); 39 | if ($time1 === $time2) 40 | { 41 | $time2 = $time1 + 1; 42 | } 43 | $sql = 'insert into test_query values(?,?,?)'; 44 | $stmt = $connection->prepare($sql); 45 | $stmt->bindParams([ 46 | ['type' => TDengine\TSDB_DATA_TYPE_TIMESTAMP, 'value' => $time2], 47 | ['type' => TDengine\TSDB_DATA_TYPE_INT, 'value' => 36], 48 | ['type' => TDengine\TSDB_DATA_TYPE_FLOAT, 'value' => 44.0], 49 | ]); 50 | $resource = $stmt->execute(); 51 | Assert::eq($resource->affectedRows(), 1); 52 | Assert::eq($resource->getSql(), $sql); 53 | 54 | $sql = 'select * from test_query order by ts desc limit 2'; 55 | $resource = $connection->query($sql); 56 | Assert::eq($resource->getSql(), $sql); 57 | Assert::eq($resource->fetch(), [ 58 | [ 59 | 'ts' => $time2, 60 | 'temperature' => 36, 61 | 'humidity' => 44.0, 62 | ], 63 | [ 64 | 'ts' => $time1, 65 | 'temperature' => 36, 66 | 'humidity' => 44.0, 67 | ], 68 | ]); 69 | 70 | $resource = $connection->query($sql); 71 | Assert::eq($resource->getSql(), $sql); 72 | Assert::eq($resource->fetchRow(), [ 73 | 'ts' => $time2, 74 | 'temperature' => 36, 75 | 'humidity' => 44.0, 76 | ]); 77 | ?> 78 | --EXPECT-- 79 | -------------------------------------------------------------------------------- /tests/01-sync/005-prepare-batch.phpt: -------------------------------------------------------------------------------- 1 | --TEST-- 2 | test prepare batch 3 | --EXTENSIONS-- 4 | tdengine 5 | --FILE-- 6 | connect(); 15 | 16 | $sql = 'create database if not exists php_tdengine_test'; 17 | $resource = $connection->query($sql); 18 | Assert::eq($resource->getSql(), $sql); 19 | 20 | $connection->selectDb('php_tdengine_test'); 21 | 22 | $sql = 'CREATE STABLE if not exists meters (ts TIMESTAMP, voltage INT) TAGS (location BINARY(64), groupId INT)'; 23 | $resource = $connection->query($sql); 24 | Assert::eq($resource->getSql(), $sql); 25 | 26 | $time1 = (int) (microtime(true) * 1000); 27 | $sql = 'INSERT INTO ? USING meters TAGS(?, ?) VALUES(?, ?)'; 28 | $stmt = $connection->prepare($sql); 29 | 30 | $stmt->setTableNameTags('d1001', [ 31 | [TDengine\TSDB_DATA_TYPE_BINARY, 'Beijing.Chaoyang'], 32 | [TDengine\TSDB_DATA_TYPE_INT, 1], 33 | ]); 34 | 35 | $stmt->bindParams([ 36 | [TDengine\TSDB_DATA_TYPE_TIMESTAMP, $time1], 37 | [TDengine\TSDB_DATA_TYPE_INT, 219], 38 | ]); 39 | 40 | $time2 = (int) (microtime(true) * 1000); 41 | if ($time1 === $time2) 42 | { 43 | $time2 = $time1 + 1; 44 | } 45 | 46 | $stmt->bindParams([ 47 | [TDengine\TSDB_DATA_TYPE_TIMESTAMP, $time2], 48 | [TDengine\TSDB_DATA_TYPE_INT, 218], 49 | ]); 50 | 51 | $resource = $stmt->execute(); 52 | Assert::eq($resource->affectedRows(), 2); 53 | Assert::eq($resource->getSql(), $sql); 54 | 55 | $sql = 'select * from meters order by ts desc limit 2'; 56 | $resource = $connection->query($sql); 57 | Assert::eq($resource->getSql(), $sql); 58 | Assert::eq($resource->fetch(), [ 59 | [ 60 | 'ts' => $time2, 61 | 'voltage' => 218, 62 | 'location' => 'Beijing.Chaoyang', 63 | 'groupid' => 1, 64 | ], 65 | [ 66 | 'ts' => $time1, 67 | 'voltage' => 219, 68 | 'location' => 'Beijing.Chaoyang', 69 | 'groupid' => 1, 70 | ], 71 | ]); 72 | 73 | $resource = $connection->query($sql); 74 | Assert::eq($resource->getSql(), $sql); 75 | Assert::eq($resource->fetchRow(), [ 76 | 'ts' => $time2, 77 | 'voltage' => 218, 78 | 'location' => 'Beijing.Chaoyang', 79 | 'groupid' => 1, 80 | ]); 81 | ?> 82 | --EXPECT-- 83 | -------------------------------------------------------------------------------- /tests/02-swoole/001-extension-loaded.phpt: -------------------------------------------------------------------------------- 1 | --TEST-- 2 | Check if tdengine is loaded 3 | --EXTENSIONS-- 4 | swoole 5 | tdengine 6 | --FILE-- 7 | 10 | --EXPECT-- 11 | The extension "tdengine" is available 12 | -------------------------------------------------------------------------------- /tests/02-swoole/002-connect-close.phpt: -------------------------------------------------------------------------------- 1 | --TEST-- 2 | test connect and close 3 | --SKIPIF-- 4 | 5 | --EXTENSIONS-- 6 | tdengine 7 | --FILE-- 8 | getHost()); 22 | assert(getPort() === $connection->getPort()); 23 | assert(getUser() === $connection->getUser()); 24 | assert(getPass() === $connection->getPass()); 25 | assert(getDb() === $connection->getDb()); 26 | $connection->connect(); 27 | assert(true === $connection->isConnected()); 28 | $connection->close(); 29 | }); 30 | ?> 31 | --EXPECT-- 32 | -------------------------------------------------------------------------------- /tests/02-swoole/003-query.phpt: -------------------------------------------------------------------------------- 1 | --TEST-- 2 | test query 3 | --SKIPIF-- 4 | 5 | --EXTENSIONS-- 6 | tdengine 7 | --FILE-- 8 | connect(); 19 | 20 | $sql = 'create database if not exists php_tdengine_test'; 21 | $resource = $connection->query($sql); 22 | Assert::eq($resource->getSql(), $sql); 23 | 24 | $connection->selectDb('php_tdengine_test'); 25 | 26 | $sql = 'create table if not exists test_query (ts timestamp, temperature int, humidity float)'; 27 | $resource = $connection->query($sql); 28 | Assert::eq($resource->getSql(), $sql); 29 | 30 | $time = (int) (microtime(true) * 1000); 31 | $sql = sprintf('insert into test_query values(%s,%s,%s)', $time, 36, 44.0); 32 | $resource = $connection->query($sql); 33 | Assert::eq($resource->affectedRows(), 1); 34 | Assert::eq($resource->getSql(), $sql); 35 | 36 | $sql = 'select * from test_query order by ts desc limit 1'; 37 | $resource = $connection->query($sql); 38 | Assert::eq($resource->getSql(), $sql); 39 | Assert::eq($resource->fetch(), [ 40 | [ 41 | 'ts' => $time, 42 | 'temperature' => 36, 43 | 'humidity' => 44.0, 44 | ], 45 | ]); 46 | 47 | $resource = $connection->query($sql); 48 | Assert::eq($resource->getSql(), $sql); 49 | Assert::eq($resource->fetchRow(), [ 50 | 'ts' => $time, 51 | 'temperature' => 36, 52 | 'humidity' => 44.0, 53 | ]); 54 | 55 | // bug: https://github.com/Yurunsoft/php-tdengine/issues/6 56 | $sql = <<<'SQL' 57 | SELECT 58 | AVG(temperature) as avg_temperature 59 | FROM 60 | test_query 61 | WHERE 62 | ts >= NOW - 1d 63 | and ts <= now INTERVAL(10s) FILL(PREV) 64 | limit 65 | 1; 66 | SQL; 67 | $resource = $connection->query($sql); 68 | Assert::eq($resource->getSql(), $sql); 69 | $row = $resource->fetchRow(); 70 | Assert::true(array_key_exists('avg_temperature', $row)); 71 | Assert::null($row['avg_temperature']); 72 | }); 73 | ?> 74 | --EXPECT-- 75 | -------------------------------------------------------------------------------- /tests/02-swoole/004-prepare.phpt: -------------------------------------------------------------------------------- 1 | --TEST-- 2 | test prepare 3 | --SKIPIF-- 4 | 5 | --EXTENSIONS-- 6 | tdengine 7 | --FILE-- 8 | connect(); 19 | 20 | $sql = 'create database if not exists php_tdengine_test'; 21 | $resource = $connection->query($sql); 22 | Assert::eq($resource->getSql(), $sql); 23 | 24 | $connection->selectDb('php_tdengine_test'); 25 | 26 | $sql = 'create table if not exists test_query (ts timestamp, temperature int, humidity float)'; 27 | $resource = $connection->query($sql); 28 | Assert::eq($resource->getSql(), $sql); 29 | 30 | $time1 = (int) (microtime(true) * 1000); 31 | $sql = 'insert into test_query values(?,?,?)'; 32 | $stmt = $connection->prepare($sql); 33 | $stmt->bindParams([ 34 | [TDengine\TSDB_DATA_TYPE_TIMESTAMP, $time1], 35 | [TDengine\TSDB_DATA_TYPE_INT, 36], 36 | [TDengine\TSDB_DATA_TYPE_FLOAT, 44.0], 37 | ]); 38 | $resource = $stmt->execute(); 39 | Assert::eq($resource->affectedRows(), 1); 40 | Assert::eq($resource->getSql(), $sql); 41 | 42 | $time2 = (int) (microtime(true) * 1000); 43 | if ($time1 === $time2) 44 | { 45 | $time2 = $time1 + 1; 46 | } 47 | $sql = 'insert into test_query values(?,?,?)'; 48 | $stmt = $connection->prepare($sql); 49 | $stmt->bindParams([ 50 | ['type' => TDengine\TSDB_DATA_TYPE_TIMESTAMP, 'value' => $time2], 51 | ['type' => TDengine\TSDB_DATA_TYPE_INT, 'value' => 36], 52 | ['type' => TDengine\TSDB_DATA_TYPE_FLOAT, 'value' => 44.0], 53 | ]); 54 | $resource = $stmt->execute(); 55 | Assert::eq($resource->affectedRows(), 1); 56 | Assert::eq($resource->getSql(), $sql); 57 | 58 | $sql = 'select * from test_query order by ts desc limit 2'; 59 | $resource = $connection->query($sql); 60 | Assert::eq($resource->getSql(), $sql); 61 | Assert::eq($resource->fetch(), [ 62 | [ 63 | 'ts' => $time2, 64 | 'temperature' => 36, 65 | 'humidity' => 44.0, 66 | ], 67 | [ 68 | 'ts' => $time1, 69 | 'temperature' => 36, 70 | 'humidity' => 44.0, 71 | ], 72 | ]); 73 | 74 | $resource = $connection->query($sql); 75 | Assert::eq($resource->getSql(), $sql); 76 | Assert::eq($resource->fetchRow(), [ 77 | 'ts' => $time2, 78 | 'temperature' => 36, 79 | 'humidity' => 44.0, 80 | ]); 81 | }); 82 | ?> 83 | --EXPECT-- 84 | -------------------------------------------------------------------------------- /tests/02-swoole/005-prepare-batch.phpt: -------------------------------------------------------------------------------- 1 | --TEST-- 2 | test prepare batch 3 | --EXTENSIONS-- 4 | tdengine 5 | --FILE-- 6 | connect(); 17 | 18 | $sql = 'create database if not exists php_tdengine_test'; 19 | $resource = $connection->query($sql); 20 | Assert::eq($resource->getSql(), $sql); 21 | 22 | $connection->selectDb('php_tdengine_test'); 23 | 24 | $sql = 'CREATE STABLE if not exists meters (ts TIMESTAMP, voltage INT) TAGS (location BINARY(64), groupId INT)'; 25 | $resource = $connection->query($sql); 26 | Assert::eq($resource->getSql(), $sql); 27 | 28 | $time1 = (int) (microtime(true) * 1000); 29 | $sql = 'INSERT INTO ? USING meters TAGS(?, ?) VALUES(?, ?)'; 30 | $stmt = $connection->prepare($sql); 31 | 32 | $stmt->setTableNameTags('d1001', [ 33 | [TDengine\TSDB_DATA_TYPE_BINARY, 'Beijing.Chaoyang'], 34 | [TDengine\TSDB_DATA_TYPE_INT, 1], 35 | ]); 36 | 37 | $stmt->bindParams([ 38 | [TDengine\TSDB_DATA_TYPE_TIMESTAMP, $time1], 39 | [TDengine\TSDB_DATA_TYPE_INT, 219], 40 | ]); 41 | 42 | $time2 = (int) (microtime(true) * 1000); 43 | if ($time1 === $time2) 44 | { 45 | $time2 = $time1 + 1; 46 | } 47 | 48 | $stmt->bindParams([ 49 | [TDengine\TSDB_DATA_TYPE_TIMESTAMP, $time2], 50 | [TDengine\TSDB_DATA_TYPE_INT, 218], 51 | ]); 52 | 53 | $resource = $stmt->execute(); 54 | Assert::eq($resource->affectedRows(), 2); 55 | Assert::eq($resource->getSql(), $sql); 56 | 57 | $sql = 'select * from meters order by ts desc limit 2'; 58 | $resource = $connection->query($sql); 59 | Assert::eq($resource->getSql(), $sql); 60 | Assert::eq($resource->fetch(), [ 61 | [ 62 | 'ts' => $time2, 63 | 'voltage' => 218, 64 | 'location' => 'Beijing.Chaoyang', 65 | 'groupid' => 1, 66 | ], 67 | [ 68 | 'ts' => $time1, 69 | 'voltage' => 219, 70 | 'location' => 'Beijing.Chaoyang', 71 | 'groupid' => 1, 72 | ], 73 | ]); 74 | 75 | $resource = $connection->query($sql); 76 | Assert::eq($resource->getSql(), $sql); 77 | Assert::eq($resource->fetchRow(), [ 78 | 'ts' => $time2, 79 | 'voltage' => 218, 80 | 'location' => 'Beijing.Chaoyang', 81 | 'groupid' => 1, 82 | ]); 83 | }); 84 | ?> 85 | --EXPECT-- 86 | -------------------------------------------------------------------------------- /tests/include/skip/skip-no-swoole.php: -------------------------------------------------------------------------------- 1 |