|
<? Php
/**
* Created by PhpStorm.
* User: yangyulong
* Date: 2015/5/26
* Time: 13: 45
*/
Class pai_db
{
Private static $ instanceof = NULL;
Public $ mongo;
Private $ host = 'localhost ';
Private $ port = '000000 ';
Private $ db;
Public $ dbname;
Private $ table = NULL;
/**
* Initialize the class to obtain the mongo instance object.
*/
Public function _ construct ($ host = NULL, $ port = NULL, $ dbname = NULL, $ table = NULL)
{
If (NULL ===$ dbname ){
$ This-> throwError ('set cannot be blank! ');
}
// Determine whether the host and port are passed
If (NULL! ==$ Host ){
$ This-> host = $ host;
}
If (NULL! ==$ Port ){
$ This-> port = $ port;
}
$ This-> table = $ table;
$ This-> mongo = new MongoClient ($ this-> host. ':'. $ this-> port );
If ($ this-> getVersion ()> = '0. 9.0 '){
$ This-> dbname = $ this-> mongo-> selectDB ($ dbname );
$ This-> db = $ this-> dbname-> selectCollection ($ table );
} Else {
$ This-> db = $ this-> mongo-> $ dbname-> $ table;
}
}
Public function getVersion ()
{
Return response client: VERSION;
}
/**
* Singleton Mode
* @ Return Mongo | null
*/
// Public static function getInstance ($ host = null, $ port = null, $ dbname = null, $ table = null ){
//
// If (! (Self: $ instanceof self )){
// Self: $ instanceof = new self ($ host, $ port, $ dbname, $ table );
//}
//
// Return self: $ instanceof;
//}
/**
* Insert a data entry
* @ Param array $ doc
*/
Public function insert ($ doc = array ())
{
If (empty ($ doc )){
$ This-> throwError ('the inserted data cannot be blank! ');
}
// Save data information
Try {
If (! $ This-> db-> insert ($ doc )){
Throw new partition exception ('data insertion failed ');
}
} Catch (except exception $ e ){
$ This-> throwError ($ e-> getMessage ());
}
}
/**
* Insert multiple data records
* @ Param array $ doc
*/
Public function insertMulti ($ doc = array ())
{
If (empty ($ doc )){
$ This-> throwError ('the inserted data cannot be blank! ');
}
// Insert data information
Foreach ($ doc as $ key => $ val ){
// Judge whether $ val is an array.
If (is_array ($ val )){
$ This-> insert ($ val );
}
}
}
/**
* Search for a record
* @ Return array | null
*/
Public function findOne ($ where = NULL)
{
If (NULL ===$ where ){
Try {
If ($ result = $ this-> db-> findOne ()){
Return $ result;
} Else {
Throw new parse exception ('data query failed ');
}
} Catch (except exception $ e ){
$ This-> throwError ($ e-> getMessage ());
}
} Else {
Try {
If ($ result = $ this-> db-> findOne ($ where )){
Return $ result;
} Else {
Throw new parse exception ('data query failed ');
}
} Catch (except exception $ e ){
$ This-> throwError ($ e-> getMessage ());
}
}
}
/**
* Perform subsequent operations with todo Conditions
* Search for all documents
* @ Return cursor
*/
Public function find ($ where = NULL)
{
If (NULL ===$ where ){
Try {
If ($ result = $ this-> db-> find ()){
} Else {
Throw new parse exception ('data query failed ');
}
} Catch (except exception $ e ){
$ This-> throwError ($ e-> getMessage ());
}
} Else {
Try {
If ($ result = $ this-> db-> find ($ where )){
} Else {
Throw new parse exception ('data query failed ');
}
} Catch (except exception $ e ){
$ This-> throwError ($ e-> getMessage ());
}
}
$ Arr = array ();
Foreach ($ result as $ id => $ val ){
$ Arr [] = $ val;
}
Return $ arr;
}
/**
* Retrieve the number of records
* @ Return int
*/
Public function getCount ()
{
Try {
If ($ count = $ this-> db-> count ()){
Return $ count;
} Else {
Throw new except exception ('total lookup failed ');
}
} Catch (except exception $ e ){
$ This-> throwError ($ e-> getMessage ());
}
}
/**
* Retrieve all databases
* @ Return array
*/
Public function getDbs ()
{
Return $ this-> mongo-> listDBs ();
}
/**
* Deleting a database
* @ Param null $ dbname
* @ Return mixed
*/
Public function dropDb ($ dbname = NULL)
{
If (NULL! ==$ Dbname ){
$ Retult = $ this-> mongo-> dropDB ($ dbname );
If ($ retult ['OK']) {
Return TRUE;
} Else {
Return FALSE;
}
}
$ This-> throwError ('Enter the name of the database to be deleted ');
}
/**
* Forcibly close the database link
*/
Public function closeDb ()
{
$ This-> mongo-> close (TRUE );
}
/**
* Output error message
* @ Param $ errorInfo error content
*/
Public function throwError ($ errorInfo = '')
{
Echo "
Die ();
}
}
|