搜索
首页数据库mysql教程Oricle 数据库操作类

Oricle 数据库操作类

Jun 07, 2016 pm 05:45 PM
datasetnbsppublicreturnthis

Oricle 操作类

define('OCI_RETURN_ALLS',OCI_BOTH + OCI_RETURN_LOBS);

class Oracle {
 /**
  * select方法返回的最大记录数
  */
 const MAX_ROW_NUM = 1000;

 /**
  * 数据查询结果集对象
  * @var object $dataSet
  */
 public $dataSet   = NULL ;

 /**
  * 数据源对象
  * @var object $ds
  */
 public $ds    = NULL ;

 /**
  * 查询的SQL语句
  * @var string $sql
  */
 public $sql    = '' ;
 
 /**
  * 执行查询的模式,值为 OCI_COMMIT_ON_SUCCESS 或 OCI_DEFAULT
  * @var string $excuteMode
  */
 public $executeMode = OCI_COMMIT_ON_SUCCESS ;

 /**
  * 构造函数
  * @param object $ds 数据库
  * @param string $sql 要初始化查询的SQL语句
  */
 function __construct($ds=NULL , $sql=NULL) {
  if (!$ds) {
   $this->error(DbException::DB_UNCONNECTED, '数据库还

未连接。');
  } else {
   $this->ds = $ds;
   if ($sql) {
    $this->open($sql);
   }
  }
 }
 
 /**
  * 释放所占用的内存
  * @param object $dataSet 需要释放资源的结果集
  * @access public
  */
 public function close($dataSet=NULL) {
  if ($dataSet) {
   @oci_free_statement($dataSet);
  } else {
   @oci_free_statement($this->dataSet);
   $this->eof = false ;
   $this->recordCount = 0 ;
   $this->recNo = -1 ;
  }
 }
 
 /**
  * 对$pass进行数据库加密,返回加密之后的值
  * @param string $pass 要加密的字符串
  * @return string
  * @access public
  */
 public function encodePassword($pass) {
  return md5($pass);
 }
 
 /**
  * 得到错误信息和错误代号
  * @param integer $queryResult 查询结果
  * @return array
  * @access protected
  */
 protected function errorInfo($queryResult = NULL) {
  $result = oci_error($this->ds->connect);
  return $result;
 }
 
 /**
  * 错误信息处理
  * @param string $errorId 错误ID
  * @param string $errorMessage 错误信息
  * @access protected
  */
 protected function error($errorId, $errorMessage) {
  throw new DbException($errorMessage, $errorId);
 }
 
 /**
  * 执行SQL语句
  * @param string $sql SQL语句
  * @return object
  * @param int $rowFrom 启始行号,行号从1开始
  * @param int $rowTo 结束行号,值为0表示
  * @access public
  * @see DbQuery::open
  */
 public function execute($sql = '', $rowFrom = 0, $rowTo =

self::MAX_ROW_NUM) {
  if ($rowFrom != 0 || $rowTo != self::MAX_ROW_NUM) {
   $sql = 'select * from (select row_.*, rownum rownum_

from ('
     . $sql . ') row_ where rownum      . $rowTo . ') where rownum_ >= ' .

$rowFrom;
  }
  //echo $sql . '
';
  //$start = microtime(true);
  $dataSet = @oci_parse($this->ds->connect, $sql);
  $executeSucceed = @oci_execute($dataSet, $this-

>executeMode);
  //echo 'sql:'. ((string)(microtime(true)-$start)) . '
';
  if (!$dataSet || !$executeSucceed) {
   $sqlError = $this->errorInfo();
   $errorMessage = '执行[' .

$sql
     . ']出错!

color=#FF0000> ['
     . $sqlError['code'] . ']: '
     . $sqlError['message'] . '

' ;
   $this->error(DbException::DB_QUERY_ERROR,

$errorMessage);
  }
  return $dataSet;
 }
 
 /**
  * 执行SQL语句,结果集保存到属性$dataSet中
  * @param string $sql SQL语句
  * @param int $rowFrom 启始行号,行号从1开始
  * @param int $rowTo 结束行号,值为0表示
  * @return object
  * @access public
  * @see DbQuery::execute
  */
 public function open($sql='', $rowFrom = 0, $rowTo =

self::MAX_ROW_NUM) {
  $this->dataSet = $this->execute($sql, $rowFrom, $rowTo);
  $this->sql = $sql ;
  return $this->dataSet;
 }

 /**
  * 将一行的各字段值拆分到一个数组中
  * @param object $dataSet 结果集
  * @param integer $resultType 返回类型,OCI_ASSOC、OCI_NUM 或

OCI_BOTH
  * @return array
  */
 public function fetchRecord($dataSet=NULL, $resultType=OCI_BOTH) {
  $result = @oci_fetch_array(($dataSet) ? $dataSet : $this-

>dataSet, $resultType);
  if (is_array($result)) {
   foreach ($result as $key => $value) {
    if (!is_numeric($key)) {
     $result[strtolower($key)] = $value;
    }
   }
  }
  return $result;
 }


 public function snext() {
  $result = @oci_fetch_array($this->dataSet, OCI_BOTH);
  return $result;
 }
 /**
  * 取得字段数量
  * @param object $dataSet 结果集
  * @return integer
  */
 public function getFieldCount($dataSet = NULL) {
  return oci_num_fields(($dataSet) ? $dataSet : $this-

>dataSet);
 }
 
 /**
  * 取得下一条记录。返回记录号,如果到了记录尾,则返回FALSE
  * @return integer
  * @access public
  * @see getPrior()
  */
 public function next() {
  return $this->fetchRecord();
 }
 
 /**
  * 得到当前数据库时间,格式为:yyyy-mm-dd hh:mm:ss
  * @return string
  * @access public
  */
 public function getNow() {
  return $this->getValue('SELECT TO_CHAR(SYSDATE, 'YYYY-MM-DD

HH24:MI:SS') dateOfNow FROM DUAL');
 }
 
 /**
  * 根据SQL语句从数据表中取数据,只取第一条记录的值,
  * 如果记录中只有一个字段,则只返回字段值。
  * 未找到返回 FALSE
  *
  * @param string $sql SQL语句
  * @return array
  * @access public
  */
 public function getValue($sql = '', $hasClob = false) {
  $dataSet = $this->execute($sql, 1, 1);
  if ($hasClob) {
   $returnType = OCI_RETURN_ALLS;
  } else {
   $returnType = OCI_BOTH;
  }
  if ($result = $this->fetchRecord($dataSet,$returnType)) {
   $fieldCount = $this->getFieldCount($dataSet);
   $this->close($dataSet);
   return ($fieldCount   } else {
   return false ;
  }
 }

 public function getSeq($seqName = '') {
  $dataSet = $this->execute('SELECT '.$seqName.'.nextval from

sys.dual');
  if ($result = $this->fetchRecord($dataSet)) {
   $fieldCount = $this->getFieldCount($dataSet);
   $this->close($dataSet);
   return ($fieldCount   } else {
   return false ;
  }
 }
 /**
  * 表是否存在,返回true
  * @param string $tableName 要查询的表名
  * @return bool
  * @access public
  */
 public function tableIsExists($tableName) {
  return false;
 }
 
 /**
  * 开始事务
  * @access public
  */
 public function begin() {
  $this->executeMode = OCI_DEFAULT;
 }
 
 /**
  * 提交事务
  * @access public
  */
 public function commit() {
  oci_commit($this->ds->connect);
  $this->executeMode = OCI_COMMIT_ON_SUCCESS;
 }
 
 /**
  * 回滚事务
  * @access public
  */
 public function rollback() {
  oci_rollback($this->ds->connect);
  $this->executeMode = OCI_COMMIT_ON_SUCCESS;
 }
 
 /**
  * 插入一条记录
  * @param string $tableName 表名
  * @param array $fieldArray 字段数组
  * @param string $whereForUnique 唯一性条件
  * @return int
  * @access public
  */
 public function insert($tableName, $fieldArray, $whereForUnique =

NULL, $clobField = NULL ) {
  if (!$tableName || !$fieldArray || !is_array($fieldArray)) {
   throw new Exception('参数 $tableName 或 $fieldArray

的值不合法!');
  }
  if ($whereForUnique) {
   $where = ' WHERE ' . $whereForUnique;
   $isExisted = $this->getValue('SELECT COUNT(*) FROM '

. $tableName . $where);
   if ($isExisted) {
    throw new DbException('记录已经存在!',

DbException::DB_RECORD_IS_EXISTED);
   }
  }
  $fieldNameList = array();
  $fieldValueList = array();
  foreach ($fieldArray as $fieldName => $fieldValue) {
   if (!is_int($fieldName)) {
    $fieldNameList[] = $fieldName;
    if ($clobField && $clobField==$fieldName) {
     $fieldValueList[] = 'EMPTY_CLOB()';
     $hasClob = true;
     $clobStr = str_replace

("''","'",$fieldValue);
    } else {
     $fieldValueList[] = ''' .

$fieldValue . ''';
    }
   }
  }
  $fieldName = implode(',', $fieldNameList);
  $fieldValue = implode(',', $fieldValueList);
  $sql = 'INSERT INTO ' . $tableName . '('
     . $fieldName . ') VALUES (' .

$fieldValue . ')';
  if ($hasClob) {
   $sql .= ' RETURNING content INTO :clob_string';
   $dataSet = oci_parse($this->ds->connect, $sql);
   $clob = oci_new_descriptor($this->ds->connect,

OCI_D_LOB);
   oci_bind_by_name($dataSet, ':clob_string', $clob, -

1, OCI_B_CLOB);
   oci_execute($dataSet, OCI_DEFAULT);
   $clob->save($clobStr);
   oci_commit($this->ds->connect);
   return $dataSet;
  } else {
   return $this->execute($sql);
  }
 }
 
 /**
  * 更新一条记录
  * @param string $tableName 表名
  * @param array $fieldArray 字段数组
  * @param string $whereForUpdate 查询条件
  * @param string $whereForUnique 唯一性条件
  * @return int
  * @access public
  */
 public function update($tableName, $fieldArray,

$whereForUpdate=NULL, $whereForUnique=NULL, $clobField = NULL) {
  if (!$tableName || !$fieldArray || !is_array($fieldArray)) {
   throw new Exception('参数 $tableName 或 $fieldArray

的值不合法!');
  }
  if ($whereForUnique) {
   $where = ' WHERE ' . $whereForUnique;
   $isExisted = $this->getValue('SELECT COUNT(*) FROM '

. $tableName . $where);
   if ($isExisted) {
    throw new DbException('记录已经存在!',

DbException::DB_RECORD_IS_EXISTED);
   }
  }
  $fieldNameValueList = array();
  foreach ($fieldArray as $fieldName => $fieldValue) {
   if (!is_int($fieldName)) {
    if ($clobField && $clobField==$fieldName) {
     $fieldNameValueList[] = $fieldName .

'=EMPTY_CLOB()';
     $hasClob = true;
     $clobStr = str_replace

("''","'",$fieldValue);
    } else {
     $fieldNameValueList[] = $fieldName .

'='' . $fieldValue . ''';
    }
   }
  }
  $fieldNameValue = implode(',', $fieldNameValueList);
  if ($whereForUpdate) {
   $whereForUpdate = ' WHERE ' . $whereForUpdate;
  }
  $sql = 'UPDATE ' . $tableName
    . ' SET ' . $fieldNameValue .

$whereForUpdate;
  if ($hasClob) {
   $sql .= ' RETURNING content INTO :clob_string';
   $dataSet = oci_parse($this->ds->connect, $sql);
   $clob = oci_new_descriptor($this->ds->connect,

OCI_D_LOB);
   oci_bind_by_name($dataSet, ':clob_string', $clob, -

1, OCI_B_CLOB);
   oci_execute($dataSet, OCI_DEFAULT);
   $clob->save($clobStr);
   oci_commit($this->ds->connect);
   return $dataSet;
  } else {
   return $this->execute($sql);
  }
 }
 
 /**
  * 选择一条记录
  * @param string $sql sql语句
  * @param string $dataFormat 返回数据格式, 值

有"array","hashmap","hashmap_str","dataset"
  * @param int $rowFrom 启始行号,行号从1开始
  * @param int $rowTo 结束行号,值为0表示
  * @result array
  * @access public
  */
 public function select($sql, $dataFormat = 'array', $rowFrom = 0,

$rowTo = self::MAX_ROW_NUM, $hasClob = false) {
  $dataSet = $this->execute($sql, $rowFrom, $rowTo);
  switch ($dataFormat) {
  case 'array': //数组
   $result = array();
   $isMultiField = ($this->getFieldCount($dataSet) >

1);
   $i = 0;
   if ($hasClob) {
    $returnType = OCI_RETURN_ALLS;
   } else {
    $returnType = OCI_BOTH;
   }
   while ($data = $this->fetchRecord

($dataSet,$returnType)) {
    $result[$i] = ($isMultiField) ? $data :

$data[0];
    $i++;
   }
   $this->close($dataSet);
   break;

  case 'hashmap': //散列表
   $result = array();
   while ($data = $this->fetchRecord($dataSet)) {
    $result[ $data[0] ] = $data[1];
   }
   $this->close($dataSet);
   break;

  case 'hashmap_str': //散列表字符串
   $result = array();
   while ($data = $this->fetchRecord($dataSet,

OCI_NUM)) {
    $result[] = $data[0] . '=' . $data[1];
   }
   $result = implode('|', $result);
   $this->close($dataSet);
   break;

  default: //dataset 数据集,当返回数据格式为数据集时,select

方法的功能与execute方法相同
   $result = $dataSet;
  }
  return $result;
 }
 
 /**
  * 返回最大值
  * @param string $tableName 表名
  * @param string $idField 字段名
  * @param string $where 查询条件
  * @return int
  * @access public
  */
 public function getMax($tableName, $idField, $where = NULL) {
  $where = ($where) ? (' WHERE ' . $where) : '';
  return $this->getValue('SELECT MAX(' . $idField . ') FROM '

. $tableName . $where);
 }
}

声明
本文内容由网友自发贡献,版权归原作者所有,本站不承担相应法律责任。如您发现有涉嫌抄袭侵权的内容,请联系admin@php.cn
MySQL:初学者的基本技能MySQL:初学者的基本技能Apr 18, 2025 am 12:24 AM

MySQL适合初学者学习数据库技能。1.安装MySQL服务器和客户端工具。2.理解基本SQL查询,如SELECT。3.掌握数据操作:创建表、插入、更新、删除数据。4.学习高级技巧:子查询和窗口函数。5.调试和优化:检查语法、使用索引、避免SELECT*,并使用LIMIT。

MySQL:结构化数据和关系数据库MySQL:结构化数据和关系数据库Apr 18, 2025 am 12:22 AM

MySQL通过表结构和SQL查询高效管理结构化数据,并通过外键实现表间关系。1.创建表时定义数据格式和类型。2.使用外键建立表间关系。3.通过索引和查询优化提高性能。4.定期备份和监控数据库确保数据安全和性能优化。

MySQL:解释的关键功能和功能MySQL:解释的关键功能和功能Apr 18, 2025 am 12:17 AM

MySQL是一个开源的关系型数据库管理系统,广泛应用于Web开发。它的关键特性包括:1.支持多种存储引擎,如InnoDB和MyISAM,适用于不同场景;2.提供主从复制功能,利于负载均衡和数据备份;3.通过查询优化和索引使用提高查询效率。

SQL的目的:与MySQL数据库进行交互SQL的目的:与MySQL数据库进行交互Apr 18, 2025 am 12:12 AM

SQL用于与MySQL数据库交互,实现数据的增、删、改、查及数据库设计。1)SQL通过SELECT、INSERT、UPDATE、DELETE语句进行数据操作;2)使用CREATE、ALTER、DROP语句进行数据库设计和管理;3)复杂查询和数据分析通过SQL实现,提升业务决策效率。

初学者的MySQL:开始数据库管理初学者的MySQL:开始数据库管理Apr 18, 2025 am 12:10 AM

MySQL的基本操作包括创建数据库、表格,及使用SQL进行数据的CRUD操作。1.创建数据库:CREATEDATABASEmy_first_db;2.创建表格:CREATETABLEbooks(idINTAUTO_INCREMENTPRIMARYKEY,titleVARCHAR(100)NOTNULL,authorVARCHAR(100)NOTNULL,published_yearINT);3.插入数据:INSERTINTObooks(title,author,published_year)VA

MySQL的角色:Web应用程序中的数据库MySQL的角色:Web应用程序中的数据库Apr 17, 2025 am 12:23 AM

MySQL在Web应用中的主要作用是存储和管理数据。1.MySQL高效处理用户信息、产品目录和交易记录等数据。2.通过SQL查询,开发者能从数据库提取信息生成动态内容。3.MySQL基于客户端-服务器模型工作,确保查询速度可接受。

mysql:构建您的第一个数据库mysql:构建您的第一个数据库Apr 17, 2025 am 12:22 AM

构建MySQL数据库的步骤包括:1.创建数据库和表,2.插入数据,3.进行查询。首先,使用CREATEDATABASE和CREATETABLE语句创建数据库和表,然后用INSERTINTO语句插入数据,最后用SELECT语句查询数据。

MySQL:一种对数据存储的初学者友好方法MySQL:一种对数据存储的初学者友好方法Apr 17, 2025 am 12:21 AM

MySQL适合初学者,因为它易用且功能强大。1.MySQL是关系型数据库,使用SQL进行CRUD操作。2.安装简单,需配置root用户密码。3.使用INSERT、UPDATE、DELETE、SELECT进行数据操作。4.复杂查询可使用ORDERBY、WHERE和JOIN。5.调试需检查语法,使用EXPLAIN分析查询。6.优化建议包括使用索引、选择合适数据类型和良好编程习惯。

See all articles

热AI工具

Undresser.AI Undress

Undresser.AI Undress

人工智能驱动的应用程序,用于创建逼真的裸体照片

AI Clothes Remover

AI Clothes Remover

用于从照片中去除衣服的在线人工智能工具。

Undress AI Tool

Undress AI Tool

免费脱衣服图片

Clothoff.io

Clothoff.io

AI脱衣机

AI Hentai Generator

AI Hentai Generator

免费生成ai无尽的。

热门文章

R.E.P.O.能量晶体解释及其做什么(黄色晶体)
1 个月前By尊渡假赌尊渡假赌尊渡假赌
R.E.P.O.最佳图形设置
1 个月前By尊渡假赌尊渡假赌尊渡假赌
威尔R.E.P.O.有交叉游戏吗?
1 个月前By尊渡假赌尊渡假赌尊渡假赌

热工具

安全考试浏览器

安全考试浏览器

Safe Exam Browser是一个安全的浏览器环境,用于安全地进行在线考试。该软件将任何计算机变成一个安全的工作站。它控制对任何实用工具的访问,并防止学生使用未经授权的资源。

WebStorm Mac版

WebStorm Mac版

好用的JavaScript开发工具

适用于 Eclipse 的 SAP NetWeaver 服务器适配器

适用于 Eclipse 的 SAP NetWeaver 服务器适配器

将Eclipse与SAP NetWeaver应用服务器集成。

MinGW - 适用于 Windows 的极简 GNU

MinGW - 适用于 Windows 的极简 GNU

这个项目正在迁移到osdn.net/projects/mingw的过程中,你可以继续在那里关注我们。MinGW:GNU编译器集合(GCC)的本地Windows移植版本,可自由分发的导入库和用于构建本地Windows应用程序的头文件;包括对MSVC运行时的扩展,以支持C99功能。MinGW的所有软件都可以在64位Windows平台上运行。

Atom编辑器mac版下载

Atom编辑器mac版下载

最流行的的开源编辑器