第一步 :添加orale.php文件
文件路径:fastadminthinkphplibrarythinkdbconnectorOracle.php
<?php// +----------------------------------------------------------------------// | ThinkPHP [ WE CAN DO IT JUST THINK ]// +----------------------------------------------------------------------// | Licensed ( http://www.apache.org/licenses/LICENSE-2.0 )// +----------------------------------------------------------------------// | Author: liu21st <liu21st@gmail.com>// +----------------------------------------------------------------------namespace thinkdbconnector;use PDO;use thinkdbConnection;use thinkLog;/** * Oracle数据库驱动 */class Oracle extends Connection{ protected $builder = '\think\oracle\Builder'; /** * 解析pdo连接的dsn信息 * @access protected * @param array $config 连接信息 * @return string */ protected function parseDsn($config) { $dsn = 'oci:dbname='; if (!empty($config['hostname'])) { // Oracle Instant Client // $dsn .= '//' . $config['hostname'] . ($config['hostport'] ? ':' . $config['hostport'] : '') . '/'; $dsn .= '(DESCRIPTION=(ADDRESS=(PROTOCOL=TCP)(HOST='.$config['hostname'] .')(PORT='.$config['hostport'].'))(CONNECT_DATA=(SID='. $config['database'].')))'; } //$dsn .= $config['database']; if (!empty($config['charset'])) { $dsn .= ';charset=' . $config['charset']; } return $dsn; } /** * 取得数据表的字段信息 * @access public * @param string $tableName * @return array */ public function getFields($tableName) { $this->initConnect(true); list($tableName) = explode(' ', $tableName); $sql = "select a.column_name,data_type,DECODE (nullable, 'Y', 0, 1) notnull,data_default, DECODE (A .column_name,b.column_name,1,0) pk from all_tab_columns a,(select column_name from all_constraints c, all_cons_columns col where c.constraint_name = col.constraint_name and c.constraint_type = 'P' and c.table_name = '" . strtoupper($tableName) . "' ) b where table_name = '" . strtoupper($tableName) . "' and a.column_name = b.column_name (+)"; $pdo = $this->linkID->query($sql); $result = $pdo->fetchAll(PDO::FETCH_ASSOC); $info = []; if ($result) { foreach ($result as $key => $val) { $val = array_change_key_case($val); $info[$val['column_name']] = [ 'name' => $val['column_name'], 'type' => $val['data_type'], 'notnull' => $val['notnull'], 'default' => $val['data_default'], 'primary' => $val['pk'], 'autoinc' => $val['pk'], ]; } } return $this->fieldCase($info); } /** * 取得数据库的表信息(暂时实现取得用户表信息) * @access public * @param string $dbName * @return array */ public function getTables($dbName = '') { $pdo = $this->linkID->query("select table_name from all_tables"); $result = $pdo->fetchAll(PDO::FETCH_ASSOC); $info = []; foreach ($result as $key => $val) { $info[$key] = current($val); } return $info; } /** * 获取最近插入的ID * @access public * @param string $sequence 自增序列名 * @return string */ public function getLastInsID($sequence = null) { if ($sequence === null) { return ''; } $pdo = $this->linkID->query("select {$sequence}.currval as id from dual"); $result = $pdo->fetchColumn(); return $result; } /** * SQL性能分析 * @access protected * @param string $sql * @return array */ protected function getExplain($sql) { return []; } protected function supportSavepoint() { return true; }}
第二步 :增加文件 dataoracle.php
文件路径:fastadminoilapplicationdataoracle.php
<?phpreturn [ // 数据库类型 'type' => 'oracle', // 服务器地址 'hostname' => '127.0.0.1', // 数据库名 'database' => 'ORCLt', // 用户名 'username' => 'HR', // 密码 'password' => 'test123', // 端口 'hostport' => '1521', // 连接dsn 'dsn' => '', // 数据库连接参数 'params' => [], // 数据库编码默认采用utf8 'charset' => 'utf8', // 数据库表前缀 'prefix' => '', // 数据库调试模式 'debug' => true, // 数据库部署方式:0 集中式(单一服务器),1 分布式(主从服务器) 'deploy' => 0, // 数据库读写是否分离 主从式有效 'rw_separate' => false, // 读写分离后 主服务器数量 'master_num' => 1, // 指定从服务器序号 'slave_no' => '', // 是否严格检查字段是否存在 'fields_strict' => true, // 数据集返回类型 'resultset_type' => 'array', // 自动写入时间戳字段 'auto_timestamp' => false, // 时间字段取出后的默认时间格式 'datetime_format' => 'Y-m-d H:i:s', // 是否需要进行SQL性能分析 'sql_explain' => false,];
第三步 在配置文件引入:
第四步:测试代码:
public function index(){ $db = Db::connect('db_oracle'); halt($db); $oracle_data = $db->query('SELECT "HR"."RV_PWEL_DAY_ALLOC".*,ROWID "NAVICAT_ROWID" FROM "HR"."RV_PWEL_DAY_ALLOC" OFFSET 0 ROWS FETCH NEXT 1000 ROWS ONLY'); halt($oracle_data); $dbconn = oci_connect('system', 'test123', "(DESCRIPTION = (ADDRESS_LIST =(ADDRESS = (PROTOCOL = TCP)(HOST = 127.0.0.1)(PORT = 1521)))(CONNECT_DATA =(SERVICE_NAME = ORCLt)))"); if ($dbconn != false) { } else { echo "连接失败"; } $sqle = 'SELECT "HR"."RV_PWEL_DAY_ALLOC".*,ROWID "NAVICAT_ROWID" FROM "HR"."RV_PWEL_DAY_ALLOC" OFFSET 0 ROWS FETCH NEXT 1000 ROWS ONLY'; $ora_testr = oci_parse($dbconn, $sqle); //编译sql语句 oci_execute($ora_testr, OCI_DEFAULT); //执行 $h = 0; while ($r = oci_fetch_row($ora_testr)) //取回结果 { echo $r[0]; }}
写在最后:让fastadmin支持sqlservel 请在我的文章里面找
希望以上内容对你有所帮助!如果还有其他问题,请随时提问。 各类知识收集 拥有多年CMS企业建站经验,对 iCMS, Fastadmin, ClassCMS, LeCMS, PbootCMS, PHPCMS, 易优CMS, YzmCMS, 讯睿CMS, 极致CMS, Wordpress, HkCMS, YznCMS, WellCMS, ThinkCMF, 等各类cms的相互转化,程序开发,网站制作,bug修复,程序杀毒,插件定制都可以提供最佳解决方案。