├── .gitignore ├── LICENSE ├── README.md ├── bootstrap.php ├── composer.json ├── phpunit.xml ├── src ├── Builder.php └── Oracle.php └── test ├── CreateTest.php ├── DeleteTest.php ├── InsertTest.php ├── SelectTest.php ├── UpdateTest.php └── index.php /.gitignore: -------------------------------------------------------------------------------- 1 | .idea 2 | database.php 3 | .vscode/ 4 | log/ 5 | vendor/ 6 | oldtest/ 7 | composer.lock 8 | .gitconfig 9 | -------------------------------------------------------------------------------- /LICENSE: -------------------------------------------------------------------------------- 1 | Apache License 2 | Version 2.0, January 2004 3 | http://www.apache.org/licenses/ 4 | 5 | TERMS AND CONDITIONS FOR USE, REPRODUCTION, AND DISTRIBUTION 6 | 7 | 1. Definitions. 8 | 9 | "License" shall mean the terms and conditions for use, reproduction, 10 | and distribution as defined by Sections 1 through 9 of this document. 11 | 12 | "Licensor" shall mean the copyright owner or entity authorized by 13 | the copyright owner that is granting the License. 14 | 15 | "Legal Entity" shall mean the union of the acting entity and all 16 | other entities that control, are controlled by, or are under common 17 | control with that entity. For the purposes of this definition, 18 | "control" means (i) the power, direct or indirect, to cause the 19 | direction or management of such entity, whether by contract or 20 | otherwise, or (ii) ownership of fifty percent (50%) or more of the 21 | outstanding shares, or (iii) beneficial ownership of such entity. 22 | 23 | "You" (or "Your") shall mean an individual or Legal Entity 24 | exercising permissions granted by this License. 25 | 26 | "Source" form shall mean the preferred form for making modifications, 27 | including but not limited to software source code, documentation 28 | source, and configuration files. 29 | 30 | "Object" form shall mean any form resulting from mechanical 31 | transformation or translation of a Source form, including but 32 | not limited to compiled object code, generated documentation, 33 | and conversions to other media types. 34 | 35 | "Work" shall mean the work of authorship, whether in Source or 36 | Object form, made available under the License, as indicated by a 37 | copyright notice that is included in or attached to the work 38 | (an example is provided in the Appendix below). 39 | 40 | "Derivative Works" shall mean any work, whether in Source or Object 41 | form, that is based on (or derived from) the Work and for which the 42 | editorial revisions, annotations, elaborations, or other modifications 43 | represent, as a whole, an original work of authorship. For the purposes 44 | of this License, Derivative Works shall not include works that remain 45 | separable from, or merely link (or bind by name) to the interfaces of, 46 | the Work and Derivative Works thereof. 47 | 48 | "Contribution" shall mean any work of authorship, including 49 | the original version of the Work and any modifications or additions 50 | to that Work or Derivative Works thereof, that is intentionally 51 | submitted to Licensor for inclusion in the Work by the copyright owner 52 | or by an individual or Legal Entity authorized to submit on behalf of 53 | the copyright owner. For the purposes of this definition, "submitted" 54 | means any form of electronic, verbal, or written communication sent 55 | to the Licensor or its representatives, including but not limited to 56 | communication on electronic mailing lists, source code control systems, 57 | and issue tracking systems that are managed by, or on behalf of, the 58 | Licensor for the purpose of discussing and improving the Work, but 59 | excluding communication that is conspicuously marked or otherwise 60 | designated in writing by the copyright owner as "Not a Contribution." 61 | 62 | "Contributor" shall mean Licensor and any individual or Legal Entity 63 | on behalf of whom a Contribution has been received by Licensor and 64 | subsequently incorporated within the Work. 65 | 66 | 2. Grant of Copyright License. Subject to the terms and conditions of 67 | this License, each Contributor hereby grants to You a perpetual, 68 | worldwide, non-exclusive, no-charge, royalty-free, irrevocable 69 | copyright license to reproduce, prepare Derivative Works of, 70 | publicly display, publicly perform, sublicense, and distribute the 71 | Work and such Derivative Works in Source or Object form. 72 | 73 | 3. Grant of Patent License. Subject to the terms and conditions of 74 | this License, each Contributor hereby grants to You a perpetual, 75 | worldwide, non-exclusive, no-charge, royalty-free, irrevocable 76 | (except as stated in this section) patent license to make, have made, 77 | use, offer to sell, sell, import, and otherwise transfer the Work, 78 | where such license applies only to those patent claims licensable 79 | by such Contributor that are necessarily infringed by their 80 | Contribution(s) alone or by combination of their Contribution(s) 81 | with the Work to which such Contribution(s) was submitted. If You 82 | institute patent litigation against any entity (including a 83 | cross-claim or counterclaim in a lawsuit) alleging that the Work 84 | or a Contribution incorporated within the Work constitutes direct 85 | or contributory patent infringement, then any patent licenses 86 | granted to You under this License for that Work shall terminate 87 | as of the date such litigation is filed. 88 | 89 | 4. Redistribution. You may reproduce and distribute copies of the 90 | Work or Derivative Works thereof in any medium, with or without 91 | modifications, and in Source or Object form, provided that You 92 | meet the following conditions: 93 | 94 | (a) You must give any other recipients of the Work or 95 | Derivative Works a copy of this License; and 96 | 97 | (b) You must cause any modified files to carry prominent notices 98 | stating that You changed the files; and 99 | 100 | (c) You must retain, in the Source form of any Derivative Works 101 | that You distribute, all copyright, patent, trademark, and 102 | attribution notices from the Source form of the Work, 103 | excluding those notices that do not pertain to any part of 104 | the Derivative Works; and 105 | 106 | (d) If the Work includes a "NOTICE" text file as part of its 107 | distribution, then any Derivative Works that You distribute must 108 | include a readable copy of the attribution notices contained 109 | within such NOTICE file, excluding those notices that do not 110 | pertain to any part of the Derivative Works, in at least one 111 | of the following places: within a NOTICE text file distributed 112 | as part of the Derivative Works; within the Source form or 113 | documentation, if provided along with the Derivative Works; or, 114 | within a display generated by the Derivative Works, if and 115 | wherever such third-party notices normally appear. The contents 116 | of the NOTICE file are for informational purposes only and 117 | do not modify the License. You may add Your own attribution 118 | notices within Derivative Works that You distribute, alongside 119 | or as an addendum to the NOTICE text from the Work, provided 120 | that such additional attribution notices cannot be construed 121 | as modifying the License. 122 | 123 | You may add Your own copyright statement to Your modifications and 124 | may provide additional or different license terms and conditions 125 | for use, reproduction, or distribution of Your modifications, or 126 | for any such Derivative Works as a whole, provided Your use, 127 | reproduction, and distribution of the Work otherwise complies with 128 | the conditions stated in this License. 129 | 130 | 5. Submission of Contributions. Unless You explicitly state otherwise, 131 | any Contribution intentionally submitted for inclusion in the Work 132 | by You to the Licensor shall be under the terms and conditions of 133 | this License, without any additional terms or conditions. 134 | Notwithstanding the above, nothing herein shall supersede or modify 135 | the terms of any separate license agreement you may have executed 136 | with Licensor regarding such Contributions. 137 | 138 | 6. Trademarks. This License does not grant permission to use the trade 139 | names, trademarks, service marks, or product names of the Licensor, 140 | except as required for reasonable and customary use in describing the 141 | origin of the Work and reproducing the content of the NOTICE file. 142 | 143 | 7. Disclaimer of Warranty. Unless required by applicable law or 144 | agreed to in writing, Licensor provides the Work (and each 145 | Contributor provides its Contributions) on an "AS IS" BASIS, 146 | WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or 147 | implied, including, without limitation, any warranties or conditions 148 | of TITLE, NON-INFRINGEMENT, MERCHANTABILITY, or FITNESS FOR A 149 | PARTICULAR PURPOSE. You are solely responsible for determining the 150 | appropriateness of using or redistributing the Work and assume any 151 | risks associated with Your exercise of permissions under this License. 152 | 153 | 8. Limitation of Liability. In no event and under no legal theory, 154 | whether in tort (including negligence), contract, or otherwise, 155 | unless required by applicable law (such as deliberate and grossly 156 | negligent acts) or agreed to in writing, shall any Contributor be 157 | liable to You for damages, including any direct, indirect, special, 158 | incidental, or consequential damages of any character arising as a 159 | result of this License or out of the use or inability to use the 160 | Work (including but not limited to damages for loss of goodwill, 161 | work stoppage, computer failure or malfunction, or any and all 162 | other commercial damages or losses), even if such Contributor 163 | has been advised of the possibility of such damages. 164 | 165 | 9. Accepting Warranty or Additional Liability. While redistributing 166 | the Work or Derivative Works thereof, You may choose to offer, 167 | and charge a fee for, acceptance of support, warranty, indemnity, 168 | or other liability obligations and/or rights consistent with this 169 | License. However, in accepting such obligations, You may act only 170 | on Your own behalf and on Your sole responsibility, not on behalf 171 | of any other Contributor, and only if You agree to indemnify, 172 | defend, and hold each Contributor harmless for any liability 173 | incurred by, or claims asserted against, such Contributor by reason 174 | of your accepting any such warranty or additional liability. 175 | 176 | END OF TERMS AND CONDITIONS 177 | 178 | APPENDIX: How to apply the Apache License to your work. 179 | 180 | To apply the Apache License to your work, attach the following 181 | boilerplate notice, with the fields enclosed by brackets "[]" 182 | replaced with your own identifying information. (Don't include 183 | the brackets!) The text should be enclosed in the appropriate 184 | comment syntax for the file format. We also recommend that a 185 | file or class name and description of purpose be included on the 186 | same "printed page" as the copyright notice for easier 187 | identification within third-party archives. 188 | 189 | Copyright [yyyy] [name of copyright owner] 190 | 191 | Licensed under the Apache License, Version 2.0 (the "License"); 192 | you may not use this file except in compliance with the License. 193 | You may obtain a copy of the License at 194 | 195 | http://www.apache.org/licenses/LICENSE-2.0 196 | 197 | Unless required by applicable law or agreed to in writing, software 198 | distributed under the License is distributed on an "AS IS" BASIS, 199 | WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. 200 | See the License for the specific language governing permissions and 201 | limitations under the License. 202 | -------------------------------------------------------------------------------- /README.md: -------------------------------------------------------------------------------- 1 | # think-oracle 2 | 用于 thinkphp6 框架上的基于 OCI 的 Oracle 数据库驱动。 3 | 4 | 由于官方很久没有对 PDO_OCI 更新了,其驱动源码默认不支持 Oracle 11g 及以上版本的数据库,当然也可在编译前修改 config.m4 文件使之支持。但安装之后,PDO_OCI 使用中却存在问题,如果数据库中存储中文,查询后会出现字符截断,无法得到预期的结果。 5 | 6 | 本库使用基于 OCI API 封装的 PDO 接口数据库驱动 [misuoka\ocipdo](https://github.com/misuoka/ocipdo),用来对 Oracle 数据库进行操作。 7 | 8 | 根据 Oracle 数据库的特性,对 thinkphp6 的数据库访问层进行稍作修改,使之适用于 Oracle 数据库,以便在 thinkphp6 框架中以其原有方式完美操作 Oracle 数据库。如果你使用的是 thinkphp5.1 框架,请安装 1.x.x 版本。 9 | 10 | > 有关 PDO_OCI 字符截断问题的链接:https://my.oschina.net/startphp/blog/195333 11 | 12 | ## 使用方法 13 | 14 | 使用 composer 进行安装 `composer require misuoka/think-oracle` 15 | 16 | 安装完成后,在 thinkphp6 的数据库配置文件 database.php 中,进行如下配置: 17 | 18 | ```php 19 | $config = [ 20 | // 数据库连接配置信息 21 | 'connections' => [ 22 | 'oracle' => [ 23 | // 数据库类型 24 | 'type' => '\misuoka\think\Oracle', 25 | // 服务器地址 26 | 'hostname' => '', // 填写数据库 IP 地址 27 | // 数据库名 28 | 'database' => '', // 数据库实例 SID 名称,如 ORCL 29 | // 用户名 30 | 'username' => '', // 用户名 31 | // 密码 32 | 'password' => '', // 密码 33 | // 端口 34 | 'hostport' => '', // 端口号,如 1521 35 | // 数据库连接参数 36 | 'params' => [], 37 | // 数据库编码默认采用utf8 38 | 'charset' => 'utf8', 39 | // 数据库表前缀 40 | 'prefix' => '', 41 | // 自增序列名前缀(新增的,针对 Oracle 特有的),除前缀外,名称与表名一致。如果不是,请在新增数据时使用 sequence 方法设置序列 42 | 'prefix_sequence' => '', 43 | ], 44 | ], 45 | ]; 46 | ``` 47 | 48 | 配置完成后,即可在PHP业务代码中,按 thinkphp6 官方开发手册的方法使用。 49 | 50 | ## 变更之处 51 | 52 | - 由官方的 PDO 驱动连接变更为 [misuoka\ocipdo](https://github.com/misuoka/ocipdo) 驱动连接 53 | - 更改对 Oracle 存储过程调用的判断 54 | - 获取 LastInserID 的修改 55 | - 增加了序列名的自动获取,如果用户配置了序列前缀,则根据规则(序列前缀 + 去掉表前缀的表名)自动获取序列名称,如果存在则返回序列名 56 | - 如果用户查询设置了返回自增ID,但又不显示填写序列名并且自动获取序列名失败,则结果返回 -1 57 | - 去掉 REPLACE 功能,Oracle 没有该用法 58 | - 修改 insertAll 方法,使之适用于 Oracle 的批量插入 59 | - 对数据表及字段都转大写后加`"`,避免遇到系统关键字,导致 SQL 处理错误。如:用户ID为UID,如果删除时(`DELETE TB_USER WHERE UID < 100`)UID不加上双引号,这会导致全部数据被删除,经过处理后的语句(`DELETE "TB_USER" WHERE "UID" < 100`)只会删除 UID 小于100的数据。 60 | 61 | 62 | ## thinkphp5.1 上安装方式 63 | 64 | 使用 composer 进行安装 `composer require misuoka/think-oracle 1.x` 65 | 66 | 安装完成后,在 thinkphp6 的数据库配置文件 database.php 中,进行如下配置: 67 | 68 | ```php 69 | $config = [ 70 | // 数据库类型 71 | 'type' => '\misuoka\think\Oracle', 72 | // Query类 73 | 'query' => '\misuoka\think\Query', // 如果是在 database.php 中配置,不需要填写此项,但如果是这种用法 Db::connect($config),请填写此项 74 | // 服务器地址 75 | 'hostname' => '', // 填写数据库 IP 地址 76 | // 数据库名 77 | 'database' => '', // 数据库实例名称,如 ORCL 78 | // 用户名 79 | 'username' => '', // 用户名 80 | // 密码 81 | 'password' => '', // 密码 82 | // 端口 83 | 'hostport' => '', // 端口号,如 1521 84 | // 连接dsn 85 | 'dsn' => '', // 不填写,如果填写,则数据库连接将以此为连接串,将忽略除账号密码外的参数 86 | // 数据库连接参数 87 | 'params' => [], 88 | // 数据库编码默认采用utf8 89 | 'charset' => 'utf8', 90 | // 数据库表前缀 91 | 'prefix' => '', 92 | // 自增序列名前缀(新增的,针对 Oracle 特有的),除前缀外,名称与表名一致。如果不是,请在新增数据时使用 sequence 设置序列 93 | 'prefix_sequence' => '', 94 | ]; 95 | ``` 96 | 97 | 配置完成后,即可在PHP业务代码中,按 thinkphp5.1 官方开发手册的方法使用。 -------------------------------------------------------------------------------- /bootstrap.php: -------------------------------------------------------------------------------- 1 | =7.1.0", 15 | "topthink/think-orm": "~2.0.34", 16 | "misuoka/ocipdo": "~0.1.1" 17 | }, 18 | "require-dev": { 19 | "phpunit/phpunit": "^6.2" 20 | }, 21 | "autoload": { 22 | "psr-4": { 23 | "misuoka\\think\\" : "src" 24 | } 25 | } 26 | } 27 | -------------------------------------------------------------------------------- /phpunit.xml: -------------------------------------------------------------------------------- 1 | 2 | 3 | 4 | test/CreateTest.php 5 | test/InsertTest.php 6 | test/UpdateTest.php 7 | test/SelectTest.php 8 | test/DeleteTest.php 9 | 10 | 11 | -------------------------------------------------------------------------------- /src/Builder.php: -------------------------------------------------------------------------------- 1 | getOptions(); 30 | 31 | // 获取绑定信息 32 | $bind = $query->getFieldsBindType(); 33 | 34 | // 获取合法的字段 35 | if (empty($options['field']) || '*' == $options['field']) { 36 | $allowFields = array_keys($bind); 37 | } else { 38 | $allowFields = $options['field']; 39 | } 40 | 41 | $fields = []; 42 | $values = []; 43 | 44 | foreach ($dataSet as $k => $data) { 45 | $data = $this->parseData($query, $data, $allowFields, $bind); 46 | 47 | $values[] = 'SELECT ' . implode(',', array_values($data)) . ' from dual'; // 改为 oracle 的 48 | 49 | if (!isset($insertFields)) { 50 | $insertFields = array_keys($data); 51 | } 52 | } 53 | 54 | foreach ($insertFields as $field) { 55 | $fields[] = $this->parseKey($query, $field); 56 | } 57 | 58 | return str_replace( 59 | ['%INSERT%', '%TABLE%', '%EXTRA%', '%FIELD%', '%DATA%', '%COMMENT%'], 60 | [ 61 | !empty($options['replace']) ? 'REPLACE' : 'INSERT', 62 | $this->parseTable($query, $options['table']), 63 | $this->parseExtra($query, $options['extra']), 64 | implode(' , ', $fields), 65 | implode(' UNION ALL ', $values), 66 | $this->parseComment($query, $options['comment']), 67 | ], 68 | $this->insertAllSql); 69 | } 70 | 71 | /** 72 | * 字段和表名处理 73 | * @access public 74 | * @param Query $query 查询对象 75 | * @param mixed $key 字段名 76 | * @param bool $strict 严格检测 77 | * @return string 78 | */ 79 | public function parseKey(\think\db\Query $query, $key, bool $strict = false): string 80 | { 81 | // $key = trim($key); 82 | 83 | // if (strpos($key, '->') && false === strpos($key, '(')) { 84 | // // JSON字段支持 85 | // [$field, $name] = explode($key, '->'); 86 | // $key = $field . '."' . $name . '"'; 87 | // } 88 | 89 | // return $key; 90 | 91 | if (is_int($key)) { 92 | return (string) $key; 93 | } elseif ($key instanceof Raw) { 94 | return $key->getValue(); 95 | } 96 | 97 | $key = trim($key); 98 | 99 | if (strpos($key, '.') && !preg_match('/[,\'\"\(\)`\s]/', $key)) { 100 | [$table, $key] = explode('.', $key, 2); 101 | 102 | $alias = $query->getOptions('alias'); 103 | 104 | if ('__TABLE__' == $table) { 105 | $table = $query->getOptions('table'); 106 | $table = is_array($table) ? array_shift($table) : $table; 107 | } 108 | 109 | if (isset($alias[$table])) { 110 | $table = $alias[$table]; 111 | } 112 | } else { 113 | $tableName = $query->getOptions('table'); 114 | // $marks = $tableName ? $tableName != $key : true; // 表名,默认不加双引号 115 | } 116 | 117 | // 严格检测时,把 " 去掉,避免where条件中加入驼峰式字段("myPkId")而导致抛出异常,舍弃 118 | // if ($strict && !preg_match('/^[\w\.\*]+$/', str_replace('"', '', $key))) { 119 | if ($strict && !preg_match('/^[\w\.\*]+$/', $key)) { 120 | throw new Exception('not support data:' . $key); 121 | } 122 | 123 | // if ($marks && '*' != $key && !preg_match('/[,\'\"\*\(\)`.\s]/', $key)) { 124 | if ('*' != $key && !preg_match('/[,\'\"\*\(\)`.\s]/', $key)) { 125 | // $key = '"' . $key . '"'; 126 | $key = '"' . strtoupper($key) . '"'; 127 | } 128 | 129 | if (isset($table)) { 130 | // 这里出现问题,table并没有带有 . 131 | // if (strpos($table, '.')) { 132 | // // $table = str_replace('.', '"."', $table); 133 | // $table = str_replace('.', '"."', strtoupper($table)); 134 | // } 135 | 136 | // $key = '"' . $table . '".' . $key; 137 | $key = '"' . strtoupper($table) . '".' . $key; 138 | } 139 | 140 | return $key; 141 | } 142 | } -------------------------------------------------------------------------------- /src/Oracle.php: -------------------------------------------------------------------------------- 1 | getPDOStatement($sql); 61 | $result = $pdo->fetchAll(PDO::FETCH_ASSOC); 62 | $info = []; 63 | 64 | if ($result) { 65 | foreach ($result as $key => $val) { 66 | $val = array_change_key_case($val); 67 | 68 | $info[$val['column_name']] = [ 69 | 'name' => $val['column_name'], 70 | 'type' => $val['data_type'], 71 | 'notnull' => $val['notnull'], 72 | 'default' => $val['data_default'], 73 | 'primary' => $val['pk'], 74 | 'autoinc' => $val['pk'] && in_array($val['data_type'], ['integer', 'number', 'int', 'smallint']) ? 1 : 0, // 根据字段类型来确定,字符串主键哪来都自增 75 | ]; 76 | } 77 | } 78 | 79 | return $this->fieldCase($info); 80 | } 81 | 82 | /** 83 | * 获取最近插入的ID 84 | * @access public 85 | * @param BaseQuery $query 查询对象 86 | * @param string $sequence 自增序列名 87 | * @return mixed 88 | */ 89 | public function getLastInsID(\think\db\BaseQuery $query, string $sequence = null) 90 | { 91 | return $sequence ? $this->linkID->lastInsertId($sequence) : -1; 92 | // $pdo = $this->linkID->query("select {$sequence}.currval as id from dual"); 93 | // $result = $pdo->fetchColumn(); 94 | 95 | // return $result; 96 | } 97 | 98 | /** 99 | * 根据表名,表前缀,序列前缀,获取oracle数据库序列名称 100 | * 101 | * @param [type] $options 102 | * @return void 103 | */ 104 | protected function getSequence(\think\db\BaseQuery $query, array $options = null) 105 | { 106 | $sequence = $options['sequence'] ?? null; 107 | 108 | if(!$sequence) { 109 | if(!empty($options) && isset($this->config['prefix_sequence']) && $this->config['prefix_sequence'] && !empty($options['table'])) { 110 | $seqtemp = $this->config['prefix_sequence'] . str_ireplace($this->config['prefix'], "", $options['table']); 111 | $sql = "SELECT COUNT(*) SEQ_COUNT FROM user_sequences WHERE sequence_name='" . strtoupper($seqtemp) . "'"; 112 | $stmt = $this->queryPDOStatement($query, $sql, []); 113 | if ($stmt->fetchColumn()) { 114 | $sequence = $seqtemp; 115 | } 116 | } 117 | } 118 | 119 | return $sequence; 120 | } 121 | 122 | /** 123 | * 插入记录 124 | * @access public 125 | * @param BaseQuery $query 查询对象 126 | * @param boolean $getLastInsID 返回自增主键 127 | * @return mixed 128 | */ 129 | public function insert(\think\db\BaseQuery $query, bool $getLastInsID = false) 130 | { 131 | // 分析查询表达式 132 | $options = $query->parseOptions(); 133 | 134 | // 生成SQL语句 135 | $sql = $this->builder->insert($query); 136 | 137 | // 执行操作 138 | $result = '' == $sql ? 0 : $this->pdoExecute($query, $sql, $query->getBind()); 139 | 140 | if ($result) { 141 | $sequence = $this->getSequence($query, $options); // 改变获取 sequence 的方式 142 | $lastInsId = $this->getLastInsID($query, $sequence); 143 | 144 | $data = $options['data']; 145 | 146 | if ($lastInsId) { 147 | $pk = $query->getAutoInc(); 148 | if ($pk) { 149 | $data[$pk] = $lastInsId; 150 | } 151 | } 152 | 153 | $query->setOption('data', $data); 154 | 155 | $this->db->trigger('after_insert', $query); 156 | 157 | if ($getLastInsID && $lastInsId) { 158 | return $lastInsId; 159 | } 160 | } 161 | 162 | return $result; 163 | } 164 | 165 | /** 166 | * 存储过程的输入输出参数绑定 167 | * @access public 168 | * @param array $bind 要绑定的参数列表 169 | * @return void 170 | * @throws BindParamException 171 | */ 172 | protected function bindParam(array $bind): void 173 | { 174 | foreach ($bind as $key => $val) { 175 | $param = is_numeric($key) ? $key + 1 : ':' . $key; 176 | 177 | if (is_array($val)) { 178 | $result = $this->PDOStatement->bindParam($param, $bind[$key][0], $bind[$key][1], $bind[$key][2] ?? -1); 179 | // array_unshift($val, $param); 180 | // $result = call_user_func_array([$this->PDOStatement, 'bindParam'], $val); // 不支持不能引用的参数 181 | } else { 182 | $result = $this->PDOStatement->bindValue($param, $val); 183 | } 184 | 185 | if (!$result) { 186 | $param = array_shift($val); 187 | 188 | throw new BindParamException( 189 | "Error occurred when binding parameters '{$param}'", 190 | $this->config, 191 | $this->getLastsql(), 192 | $bind 193 | ); 194 | } 195 | } 196 | } 197 | } 198 | -------------------------------------------------------------------------------- /test/CreateTest.php: -------------------------------------------------------------------------------- 1 | '', 5 | // 'password' => '', 6 | // 'hostname' => '', 7 | // 'hostport' => '', 8 | // 'database' => '', 9 | 10 | use ocipdo\PDO as OCIPDO; 11 | use PHPUnit\Framework\TestCase; 12 | 13 | class CreateTest extends TestCase 14 | { 15 | protected static $pdo; 16 | protected static $tableNames = [ 17 | 'USER' => [ 18 | ['UID', 'NUMBER PRIMARY KEY'], 19 | ['USERNAME', 'VARCHAR2(64)'], 20 | ['PASSWORD', 'VARCHAR2(64)'], 21 | ['PHONE', 'VARCHAR2(11)'], 22 | ['SEX', 'NUMBER'], 23 | ['CREATE_TIME', 'VARCHAR2(19)'], 24 | ['UPDATE_TIME', 'VARCHAR2(19)'], 25 | ['DELETE_TIME', 'VARCHAR2(19)'], 26 | ['LAST_LOGIN_TIME', 'VARCHAR2(19)'], 27 | ['STATUS', 'NUMBER'], 28 | ], 29 | 'ROLE' => [ 30 | ['RID', 'NUMBER PRIMARY KEY'], 31 | ['ROLE_NAME', 'VARCHAR2(64)'], 32 | ['ROLE_REMARK', 'VARCHAR2(512)'], 33 | ['CREATE_TIME', 'VARCHAR2(19)'], 34 | ['DELETE_TIME', 'VARCHAR2(19)'], 35 | ['UPDATE_TIME', 'VARCHAR2(19)'], 36 | ['STATUS', 'NUMBER'], 37 | ], 38 | 'MENU' => [ 39 | ['RID', 'NUMBER PRIMARY KEY'], 40 | ['MENU_NAME', 'VARCHAR2(64)'], 41 | ['MENU_REMARK', 'VARCHAR2(512)'], 42 | ['CREATE_TIME', 'VARCHAR2(19)'], 43 | ['UPDATE_TIME', 'VARCHAR2(19)'], 44 | ['DELETE_TIME', 'VARCHAR2(19)'], 45 | ['STATUS', 'NUMBER'], 46 | ['URL', 'VARCHAR2(255)'], 47 | ], 48 | 'USER_ROLE' => [ 49 | ['URID', 'NUMBER PRIMARY KEY'], 50 | ['UID', 'NUMBER'], 51 | ['RID', 'NUMBER'], 52 | ], 53 | 'ROLE_MENU' => [ 54 | ['RMID', 'NUMBER PRIMARY KEY'], 55 | ['RID', 'NUMBER'], 56 | ['MID', 'NUMBER'], 57 | ] 58 | ]; 59 | 60 | protected static $tbPrefix = "MDBTB_"; // 数据表前缀 61 | protected static $tsPrefix = "MDBTS_"; // 自增序列前缀 62 | protected static $trPrefix = "MDBTR_"; // 触发器前缀 63 | 64 | public function setUp() 65 | { 66 | global $myconfig; 67 | 68 | $dsn = sprintf('oci:dbname=//%s:%d/%s', $myconfig['hostname'], $myconfig['hostport'], $myconfig['database']); 69 | self::$pdo = new OCIPDO($dsn, $myconfig['username'], $myconfig['password']); 70 | } 71 | 72 | /** 73 | * 测试创建对象 74 | * 75 | * @return void 76 | */ 77 | public function testObject() 78 | { 79 | $this->assertNotNull(self::$pdo); 80 | } 81 | 82 | /** 83 | * 测试连接 84 | * 85 | * @return null 86 | */ 87 | public function testConnection() 88 | { 89 | $this->assertNotNull(self::$pdo->getConnection()); 90 | } 91 | 92 | /** 93 | * 删除表测试 94 | * 95 | * @return void 96 | */ 97 | public function testDropTable() 98 | { 99 | // PDOException: 100 | $sql = "SELECT TABLE_NAME FROM USER_TABLES"; 101 | $stmt = self::$pdo->prepare($sql); 102 | $stmt->execute(); 103 | $result = $stmt->fetchAll(\PDO::FETCH_ASSOC); 104 | $result = array_column ($result, 'TABLE_NAME'); 105 | 106 | $ret = true; 107 | try { 108 | $data = array_keys(self::$tableNames); 109 | foreach($data as $value) { 110 | $table = self::$tbPrefix . $value; 111 | if(in_array($table, $result)) { 112 | // 执行删除语句 113 | $sql = "DROP TABLE {$table}"; 114 | $stmt = self::$pdo->prepare($sql); 115 | $stmt->execute(); 116 | } 117 | } 118 | } catch(Exception $e) { 119 | var_dump($e->getMessage()); 120 | $ret = false; 121 | } 122 | 123 | $this->assertTrue($ret); 124 | } 125 | 126 | /** 127 | * 创建数据表测试 128 | * 129 | * @return void 130 | */ 131 | public function testCreateTable() 132 | { 133 | $ret = true; 134 | try { 135 | foreach(self::$tableNames as $key => $value) { 136 | $sql = "CREATE TABLE " . self::$tbPrefix . $key; 137 | $fields = []; 138 | foreach ($value as $vo) { 139 | $fields[] = '"' . $vo[0] . '" ' . $vo[1]; 140 | } 141 | $sql .= "(" . implode(',', $fields) . ")"; 142 | 143 | $stmt = self::$pdo->prepare($sql); 144 | $stmt->execute(); 145 | } 146 | } catch (Exception $e) { 147 | var_dump($e->getMessage()); 148 | $ret = false; 149 | } 150 | 151 | $this->assertTrue($ret); 152 | } 153 | 154 | /** 155 | * 删除自增序列 156 | * 157 | * @return void 158 | */ 159 | public function testDropSequence() 160 | { 161 | global $myconfig; 162 | 163 | $sql = "SELECT SEQUENCE_NAME FROM user_sequences"; 164 | $stmt = self::$pdo->prepare($sql); 165 | $stmt->execute(); 166 | $result = $stmt->fetchAll(\PDO::FETCH_ASSOC); 167 | $result = array_column($result, 'SEQUENCE_NAME'); 168 | 169 | $ret = true; 170 | try { 171 | $data = array_keys(self::$tableNames); 172 | foreach($data as $value) { 173 | $sequence = self::$tsPrefix . $value; 174 | if(in_array($sequence, $result)) { 175 | // 执行删除语句 176 | $sql = "DROP SEQUENCE {$sequence}"; 177 | $stmt = self::$pdo->prepare($sql); 178 | $stmt->execute(); 179 | } 180 | } 181 | } catch(Exception $e) { 182 | var_dump($e->getMessage()); 183 | $ret = false; 184 | } 185 | 186 | $this->assertTrue($ret); 187 | } 188 | 189 | /** 190 | * 创建序列测试 191 | * 192 | * @return void 193 | */ 194 | public function testCreateSequence() 195 | { 196 | $ret = true; 197 | try { 198 | foreach(self::$tableNames as $key => $value) { 199 | $sql = "CREATE SEQUENCE " . (self::$tsPrefix . $key) . " INCREMENT BY 1 START WITH 1 NOMAXVALUE NOCACHE"; 200 | $stmt = self::$pdo->prepare($sql); 201 | $stmt->execute(); 202 | } 203 | } catch (Exception $e) { 204 | var_dump($e->getMessage()); 205 | $ret = false; 206 | } 207 | 208 | $this->assertTrue($ret); 209 | } 210 | 211 | /** 212 | * 创建触发器测试 213 | * 214 | * @return void 215 | */ 216 | public function testCreateTrigger() 217 | { 218 | $ret = true; 219 | try { 220 | foreach(self::$tableNames as $key => $value) { 221 | $table = self::$tbPrefix . $key; 222 | $sequence = self::$tsPrefix . $key; 223 | $trigger = self::$trPrefix . $key; 224 | $sql = "CREATE OR REPLACE 225 | TRIGGER {$trigger} BEFORE INSERT ON {$table} FOR EACH ROW WHEN (new.\"{$value[0][0]}\" IS null) 226 | BEGIN 227 | SELECT {$sequence}.nextval 228 | into:new.\"{$value[0][0]}\" 229 | FROM dual; 230 | END;"; 231 | $stmt = self::$pdo->prepare($sql); 232 | $stmt->execute(); 233 | } 234 | } catch (Exception $e) { 235 | var_dump($e->getMessage()); 236 | $ret = false; 237 | } 238 | 239 | $this->assertTrue($ret); 240 | } 241 | } 242 | -------------------------------------------------------------------------------- /test/DeleteTest.php: -------------------------------------------------------------------------------- 1 | [ 12 | 'oci' => [ 13 | 'type' => 'misuoka\\think\\Oracle', 14 | // 服务器地址 15 | 'hostname' => $myconfig['hostname'], 16 | // 数据库名 17 | 'database' => $myconfig['database'], 18 | // 数据库用户名 19 | 'username' => $myconfig['username'], 20 | // 数据库密码 21 | 'password' => $myconfig['password'], 22 | // 数据库连接端口 23 | 'hostport' => $myconfig['hostport'], 24 | // 数据库连接参数 25 | 'params' => [], 26 | // 数据库编码默认采用utf8 27 | 'charset' => 'utf8', 28 | // 数据库表前缀 29 | 'prefix' => 'mdbtb_', 30 | 31 | 'prefix_sequence' => 'mdbts_' 32 | ] 33 | ] 34 | ]); 35 | 36 | class DeleteTest extends TestCase 37 | { 38 | public function testDelete() 39 | { 40 | $res = \think\facade\Db::connect('oci')->name('role')->delete(16); // ok 41 | $this->assertEquals(1, $res); 42 | $res = \think\facade\Db::connect('oci')->name('menu')->delete([13,26,49]); // ok 43 | $this->assertEquals(3, $res); 44 | $res = \think\facade\Db::connect('oci')->name('user')->where('UID','<=', 100)->delete(); // ok 45 | $this->assertEquals(100, $res); 46 | } 47 | 48 | /** 49 | * 测试全表删除 50 | * 51 | * @return void 52 | */ 53 | public function testDeleteAll() 54 | { 55 | $res = \think\facade\Db::connect('oci')->name('role')->delete(true); // ok 56 | $this->assertEquals(20, $res); // 20 这个数字是根据之前共插入的 21 行数据及删除 1 行数据所得 57 | $res = \think\facade\Db::connect('oci')->name('menu')->delete(true); // ok 58 | $this->assertEquals(47, $res); // 50 这个数字是根据之前共插入的 50 行数据及删除 3 行数据所得 59 | $res = \think\facade\Db::connect('oci')->name('user')->delete(true); // ok 60 | $this->assertEquals(2, $res); // 2 这个数字是根据之前共插入的数据及删除 100 行数据所得 61 | } 62 | } -------------------------------------------------------------------------------- /test/InsertTest.php: -------------------------------------------------------------------------------- 1 | [ 13 | 'oci' => [ 14 | 'type' => 'misuoka\\think\\Oracle', 15 | // 服务器地址 16 | 'hostname' => $myconfig['hostname'], 17 | // 数据库名 18 | 'database' => $myconfig['database'], 19 | // 数据库用户名 20 | 'username' => $myconfig['username'], 21 | // 数据库密码 22 | 'password' => $myconfig['password'], 23 | // 数据库连接端口 24 | 'hostport' => $myconfig['hostport'], 25 | // 数据库连接参数 26 | 'params' => [], 27 | // 数据库编码默认采用utf8 28 | 'charset' => 'utf8', 29 | // 数据库表前缀 30 | 'prefix' => 'mdbtb_', 31 | 32 | 'prefix_sequence' => 'mdbts_' 33 | ] 34 | ] 35 | ]); 36 | 37 | class InsertTest extends TestCase 38 | { 39 | public function testInsert() 40 | { 41 | // ['UID', 'NUMBER PRIMARY KEY'], 42 | // ['USERNAME', 'VARCHAR2(64)'], 43 | // ['PASSWORD', 'VARCHAR2(64)'], 44 | // ['PHONE', 'VARCHAR2(11)'], 45 | // ['SEX', 'NUMBER'], 46 | // ['CREATE_TIME', 'VARCHAR2(19)'], 47 | // ['UPDATE_TIME', 'VARCHAR2(19)'], 48 | // ['DELETE_TIME', 'VARCHAR2(19)'], 49 | // ['LAST_LOGIN_TIME', 'VARCHAR2(19)'], 50 | // ['STATUS', 'NUMBER'], 51 | $data = [ 52 | 'USERNAME' => '测试用户1', 53 | 'PASSWORD' => md5('PWD' . microtime()), 54 | 'PHONE' => '138' . rand(0000, 9999) . rand(0000, 9999), 55 | 'SEX' => 1, 56 | 'CREATE_TIME' => date('Y-m-d H:i:s'), 57 | 'STATUS' => 1, 58 | ]; 59 | $res = \think\facade\Db::connect('oci')->name('user')->insert($data); 60 | if(is_bool($res)) { 61 | $this->assertFalse($res); 62 | } else { 63 | $this->assertEquals(1, $res); 64 | } 65 | 66 | $data = [ 67 | 'USERNAME' => '测试用户2', 68 | 'PASSWORD' => md5('PWD' . microtime()), 69 | 'PHONE' => '138' . rand(0000, 9999) . rand(0000, 9999), 70 | 'SEX' => 2, 71 | 'CREATE_TIME' => date('Y-m-d H:i:s'), 72 | 'STATUS' => 1, 73 | 'NOTHVAE' => '不存在的列', // 会被忽略 74 | ]; 75 | 76 | $res = \think\facade\Db::connect('oci')->name('user')->strict(false)->insert($data); 77 | if(is_bool($res)) { 78 | $this->assertFalse($res); 79 | } else { 80 | $this->assertEquals(1, $res); 81 | } 82 | } 83 | 84 | public function testInsertAll() 85 | { 86 | // 插入用户信息 87 | $data = []; 88 | for($i = 100; $i < 200; $i++) { 89 | $temp = [ 90 | 'USERNAME' => '姓名' . $i, 91 | 'PASSWORD' => md5('PWD' . microtime()), 92 | 'PHONE' => '138' . rand(0000, 9999) . rand(0000, 9999), 93 | 'SEX' => rand(1, 2), 94 | 'CREATE_TIME' => date('Y-m-d H:i:s'), 95 | 'STATUS' => 1, 96 | ]; 97 | $data[] = $temp; 98 | } 99 | 100 | $res = \think\facade\Db::connect('oci')->name('user')->insertAll($data); // ok 101 | if(is_bool($res)) { 102 | $this->assertFalse($res); 103 | } else { 104 | $this->assertEquals(100, $res); 105 | } 106 | 107 | // 'MENU' => [ 108 | // ['RID', 'NUMBER PRIMARY KEY'], 109 | // ['MENU_NAME', 'VARCHAR2(64)'], 110 | // ['MENU_REMARK', 'VARCHAR2(512)'], 111 | // ['CREATE_TIME', 'VARCHAR2(19)'], 112 | // ['UPDATE_TIME', 'VARCHAR2(19)'], 113 | // ['DELETE_TIME', 'VARCHAR2(19)'], 114 | // ['STATUS', 'NUMBER'], 115 | // ['URL', 'VARCHAR2(255)'], 116 | // ], 117 | // 插入菜单信息 118 | $data = []; 119 | for($i = 1; $i <= 50; $i++) { 120 | $temp = [ 121 | 'MENU_NAME' => '菜单' . $i, 122 | 'MENU_REMARK' => md5('MENU_REMARK' . microtime()), 123 | 'CREATE_TIME' => date('Y-m-d H:i:s'), 124 | 'STATUS' => 1, 125 | 'URL' => 'URL' . $i, 126 | ]; 127 | $data[] = $temp; 128 | } 129 | 130 | $res = \think\facade\Db::connect('oci')->name('menu')->insertAll($data); // ok 131 | if(is_bool($res)) { 132 | $this->assertFalse($res); 133 | } else { 134 | $this->assertEquals(50, $res); 135 | } 136 | 137 | // 'ROLE' => [ 138 | // ['RID', 'NUMBER PRIMARY KEY'], 139 | // ['ROLE_NAME', 'VARCHAR2(64)'], 140 | // ['ROLE_REMARK', 'VARCHAR2(512)'], 141 | // ['CREATE_TIME', 'VARCHAR2(19)'], 142 | // ['DELETE_TIME', 'VARCHAR2(19)'], 143 | // ['UPDATE_TIME', 'VARCHAR2(19)'], 144 | // ['STATUS', 'NUMBER'], 145 | // ], 146 | // 插入角色信息 147 | $data = []; 148 | for($i = 1; $i <= 20; $i++) { 149 | $temp = [ 150 | 'ROLE_NAME' => '角色' . $i, 151 | 'ROLE_REMARK' => md5('ROLE_REMARK' . microtime()), 152 | 'CREATE_TIME' => date('Y-m-d H:i:s'), 153 | 'STATUS' => 1, 154 | ]; 155 | $data[] = $temp; 156 | } 157 | 158 | $res = \think\facade\Db::connect('oci')->name('role')->insertAll($data); // ok 159 | if(is_bool($res)) { 160 | $this->assertFalse($res); 161 | } else { 162 | $this->assertEquals(20, $res); 163 | } 164 | } 165 | 166 | public function testInsertGetId() 167 | { 168 | $data = [ 169 | 'ROLE_NAME' => '我的角色', 170 | 'ROLE_REMARK' => '主键ID一定是21', 171 | 'CREATE_TIME' => date('Y-m-d H:i:s'), 172 | 'STATUS' => 1, 173 | ]; 174 | 175 | // $res = \think\facade\Db::connect('oci')->name('role')->sequence('MDBTS_ROLE')->insertGetId($data); // ok 176 | $res = \think\facade\Db::connect('oci')->name('role')->insertGetId($data); // ok 177 | $this->assertEquals(21, $res); 178 | } 179 | } 180 | -------------------------------------------------------------------------------- /test/SelectTest.php: -------------------------------------------------------------------------------- 1 | [ 12 | 'oci' => [ 13 | 'type' => 'misuoka\\think\\Oracle', 14 | // 服务器地址 15 | 'hostname' => $myconfig['hostname'], 16 | // 数据库名 17 | 'database' => $myconfig['database'], 18 | // 数据库用户名 19 | 'username' => $myconfig['username'], 20 | // 数据库密码 21 | 'password' => $myconfig['password'], 22 | // 数据库连接端口 23 | 'hostport' => $myconfig['hostport'], 24 | // 数据库连接参数 25 | 'params' => [], 26 | // 数据库编码默认采用utf8 27 | 'charset' => 'utf8', 28 | // 数据库表前缀 29 | 'prefix' => 'mdbtb_', 30 | 31 | 'prefix_sequence' => 'mdbts_' 32 | ] 33 | ] 34 | ]); 35 | 36 | class SelectTest extends TestCase 37 | { 38 | public function testFind() 39 | { 40 | $res = \think\facade\Db::connect('oci')->table('mdbtb_role')->where('rid', 6)->find(); 41 | $this->assertNotNull($res); 42 | $this->assertNotEmpty($res); 43 | $this->assertArrayHasKey('ROLE_NAME', $res); 44 | 45 | $res = \think\facade\Db::connect('oci')->name('role')->where('rid', 6)->find(); 46 | $this->assertNotNull($res); 47 | $this->assertNotEmpty($res); 48 | $this->assertArrayHasKey('ROLE_NAME', $res); 49 | } 50 | 51 | public function testFindOrFail() 52 | { 53 | $this->expectException('think\db\exception\DataNotFoundException'); 54 | $this->expectExceptionMessage('table data not Found:mdbtb_role'); 55 | 56 | $res = \think\facade\Db::connect('oci')->table('mdbtb_role')->where('rid', 1000)->findOrFail(); 57 | } 58 | 59 | public function testFindOrEmpty() 60 | { 61 | $res = \think\facade\Db::connect('oci')->table('mdbtb_role')->where('rid', 1000)->findOrEmpty(); 62 | $this->assertEmpty($res); 63 | } 64 | 65 | public function testSelect() 66 | { 67 | $res = \think\facade\Db::connect('oci')->table('mdbtb_role')->where('rid', 13)->select(); 68 | $this->assertCount(1, $res); 69 | $this->assertArrayHasKey('ROLE_NAME', $res[0]); 70 | $res = \think\facade\Db::connect('oci')->name('role')->where('rid', 13)->select(); 71 | $this->assertCount(1, $res); 72 | $this->assertArrayHasKey('ROLE_NAME', $res[0]); 73 | $res = \think\facade\Db::connect('oci')->table('mdbtb_role')->where('rid', 1000)->select(); 74 | $this->assertEmpty($res); 75 | } 76 | 77 | public function testSelectOrFail() 78 | { 79 | $this->expectException('think\db\exception\DataNotFoundException'); 80 | $this->expectExceptionMessage('table data not Found:mdbtb_role'); 81 | // $res = \think\facade\Db::connect('oci')->table('mdbtb_role')->where('RID', 1000)->select(false); 82 | $res = \think\facade\Db::connect('oci')->table('mdbtb_role')->where('RID', 1000)->selectOrFail(); 83 | } 84 | 85 | public function testValue() 86 | { 87 | $res = \think\facade\Db::connect('oci')->table('mdbtb_role')->where('RID', 13)->value('ROLE_NAME'); 88 | $this->assertNotEmpty($res); 89 | $res = \think\facade\Db::connect('oci')->table('mdbtb_role')->where('RID', 500)->value('ROLE_NAME'); 90 | $this->assertNull($res); 91 | } 92 | 93 | public function testColumn() 94 | { 95 | $res = \think\facade\Db::connect('oci')->table('mdbtb_role')->where('rid', 13)->column('ROLE_NAME'); // ok 96 | $this->assertNotEmpty($res); 97 | $res = \think\facade\Db::connect('oci')->table('mdbtb_role')->where('rid', 13)->column('ROLE_NAME','RID'); // ok 98 | $this->assertNotEmpty($res); 99 | $res = \think\facade\Db::connect('oci')->table('mdbtb_role')->where('rid', '>', 13)->column('ROLE_NAME','RID'); // ok 100 | $this->assertNotEmpty($res); 101 | $res = \think\facade\Db::connect('oci')->table('mdbtb_role')->where('rid', 13)->column('*','RID'); // ok 102 | $this->assertNotEmpty($res); 103 | } 104 | 105 | public function testCount() 106 | { 107 | $res = \think\facade\Db::connect('oci')->name('menu')->whereNotNull('UPDATE_TIME')->count(); 108 | $this->assertEquals(2, $res); 109 | } 110 | 111 | public function testChunk() 112 | { 113 | \think\facade\Db::connect('oci')->table('mdbtb_user')->chunk(100, function($users) { 114 | foreach ($users as $user) { 115 | $this->assertArrayHasKey('USERNAME', $user); 116 | } 117 | }); 118 | } 119 | 120 | public function testCursor() 121 | { 122 | $cursor = \think\facade\Db::connect('oci')->table('mdbtb_user')->where('STATUS', 1)->cursor(); 123 | foreach ($cursor as $user) { 124 | $this->assertArrayHasKey('USERNAME', $user); 125 | } 126 | } 127 | } 128 | -------------------------------------------------------------------------------- /test/UpdateTest.php: -------------------------------------------------------------------------------- 1 | [ 12 | 'oci' => [ 13 | 'type' => 'misuoka\\think\\Oracle', 14 | // 服务器地址 15 | 'hostname' => $myconfig['hostname'], 16 | // 数据库名 17 | 'database' => $myconfig['database'], 18 | // 数据库用户名 19 | 'username' => $myconfig['username'], 20 | // 数据库密码 21 | 'password' => $myconfig['password'], 22 | // 数据库连接端口 23 | 'hostport' => $myconfig['hostport'], 24 | // 数据库连接参数 25 | 'params' => [], 26 | // 数据库编码默认采用utf8 27 | 'charset' => 'utf8', 28 | // 数据库表前缀 29 | 'prefix' => 'mdbtb_', 30 | 31 | 'prefix_sequence' => 'mdbts_' 32 | ] 33 | ] 34 | ]); 35 | 36 | class UpdateTest extends TestCase 37 | { 38 | public function testUpdate() 39 | { 40 | $res = \think\facade\Db::connect('oci')->name('menu')->where('rid', 6)->update(['MENU_NAME' => '好菜单6', 'UPDATE_TIME' => date('Y-m-d H:i:s')]); 41 | $this->assertNotNull($res); 42 | $this->assertNotEmpty($res); 43 | $this->assertEquals(1, $res); 44 | 45 | $res = \think\facade\Db::connect('oci')->name('menu')->where('rid', 33)->data(['MENU_NAME' => '真菜单33', 'UPDATE_TIME' => date('Y-m-d H:i:s')])->update(); 46 | $this->assertNotNull($res); 47 | $this->assertNotEmpty($res); 48 | $this->assertEquals(1, $res); 49 | } 50 | } -------------------------------------------------------------------------------- /test/index.php: -------------------------------------------------------------------------------- 1 | [ 11 | 'oci' => [ 12 | 'type' => 'misuoka\\think\\Oracle', 13 | // 服务器地址 14 | 'hostname' => $myconfig['hostname'], 15 | // 数据库名 16 | 'database' => $myconfig['database'], 17 | // 数据库用户名 18 | 'username' => $myconfig['username'], 19 | // 数据库密码 20 | 'password' => $myconfig['password'], 21 | // 数据库连接端口 22 | 'hostport' => $myconfig['hostport'], 23 | // 数据库连接参数 24 | 'params' => [], 25 | // 数据库编码默认采用utf8 26 | 'charset' => 'utf8', 27 | // 数据库表前缀 28 | 'prefix' => 'mdbtb_', 29 | 30 | 'prefix_sequence' => 'mdbts_' 31 | ] 32 | ] 33 | ]); 34 | 35 | $res = \think\facade\Db::connect('oci')->name('user') 36 | ->alias('u') 37 | ->leftJoin('UserRole ur', 'u.uid = ur.uid') 38 | // ->leftJoin('mdbtb_user_role ur', 'u.uid = ur.uid') 39 | // ->leftJoin(['MDBTB_USER_ROLE' => 'ur'], 'u.uid = ur.uid') 40 | ->where('U.uId', 1) 41 | ->fetchSql(true) 42 | ->select(); 43 | 44 | // $res = \think\facade\Db::connect('oci')->name('user')->where('"UID"','<=', 100)->fetchSql(true)->delete(); // ok 45 | echo $res; 46 | 47 | --------------------------------------------------------------------------------