Provides various official and user-released code examples. For more information, see ThinkPHP3.2.3 +.
// + ----------------------------------------------------------------------
// | ThinkPHP [we can do it just think it]
// + ----------------------------------------------------------------------
// | Copyright (c) 2006-2014 http://thinkphp1.cn All rights reserved.
// + ----------------------------------------------------------------------
// | Licensed (http://www.apache.org/licenses/LICENSE-2.0)
// + ----------------------------------------------------------------------
// | Author: liu21st
// + ----------------------------------------------------------------------
Namespace Think \ Db \ Driver;
Use Think \ Db \ Driver;
Use PDO;
/**
* Firebird Database driver
*/
Class Firebird extends Driver {
Protected $ selectSql = 'select % LIMIT % DISTINCT % FIELD % FROM % TABLE % JOIN % WHERE % GROUP % HAVING % ORDER % ';
/**
* Parse the dsn information of the pdo connection
* @ Access public
* @ Param array $ config connection information
* @ Return string
*/
Protected function parseDsn ($ config ){
$ Dsn = 'firebird: dbname = '. $ config ['hostname'].'/'. ($ config ['hostport']? : 3050). ':'. $ config ['database'];
Return $ dsn;
}
/**
* Database connection method
* @ Access public
*/
Public function connect ($ config = '', $ linkNum = 0 ){
If (! Isset ($ this-> linkID [$ linkNum]) {
If (empty ($ config) $ config = $ this-> config;
Try {
If (empty ($ config ['dsn ']) {
$ Config ['dsn '] = $ this-> parseDsn ($ config );
}
$ This-> linkID [$ linkNum] = new PDO ($ config ['dsn '], $ config ['username'], $ config ['Password']);
} Catch (\ PDOException $ e ){
E ($ e-> getMessage ());
}
}
Return $ this-> linkID [$ linkNum];
}
/**
* Execution statement
* @ Access public
* @ Param string $ str SQL command
* @ Param boolean $ fetchSql does not execute but only obtain SQL
* @ Return mixed
*/
Public function execute ($ str, $ fetchSql = false ){
$ This-> initConnect (true );
If (! $ This-> _ linkID) return false;
$ This-> queryStr = $ str;
If (! Empty ($ this-> bind )){
$ That = $ this;
$ This-> queryStr = strtr ($ this-> queryStr, array_map (function ($ val) use ($ that) {return '\''. $ that-> escapeString ($ val ). '\ ''' ;}, $ this-> bind ));
}
If ($ fetchSql ){
Return $ this-> queryStr;
}
// Release the previous Query Result
If (! Empty ($ this-> PDOStatement) $ this-> free ();
$ This-> executeTimes ++;
N ('db _ write', 1); // compatible with the code
// Record execution start time
$ This-> debug (true );
$ This-> PDOStatement = $ this-> _ linkID-> prepare ($ str );
If (false ===$ this-> PDOStatement ){
E ($ this-> error ());
}
Foreach ($ this-> bind as $ key => $ val ){
If (is_array ($ val )){
$ This-> PDOStatement-> bindValue ($ key, $ val [0], $ val [1]);
} Else {
$ This-> PDOStatement-> bindValue ($ key, $ val );
}
}
$ This-> bind = array ();
$ Result = $ this-> PDOStatement-> execute ();
$ This-> debug (false );
If (false ===$ result ){
$ This-> error ();
Return false;
} Else {
$ This-> numRows = $ this-> PDOStatement-> rowCount ();
Return $ this-> numRows;
}
}
/**
* Obtain the field information of the data table.
* @ Access public
*/
Public function getFields ($ tableName ){
$ This-> initConnect (true );
List ($ tableName) = explode ('', $ tableName );
$ SQL = 'select RF. RDB $ FIELD_NAME as field, RF. RDB $ DEFAULT_VALUE AS DEFAULT1, RF. RDB $ NULL_FLAG AS NULL1, TRIM (T. RDB $ TYPE_NAME) | \ '(\' | F. RDB $ FIELD_LENGTH | \ ') \' as type from rdb $ RELATION_FIELDS rf left join rdb $ fields f on (F. RDB $ FIELD_NAME = RF. RDB $ FIELD_SOURCE) left join rdb $ types t on (T. RDB $ TYPE = F. RDB $ FIELD_TYPE) where rdb $ RELATION_NAME = UPPER (\''. $ tableName. '\') and t. RDB $ FIELD_NAME = \ 'rdb $ FIELD_TYPE \ 'order By RDB $ FIELD_POSITION ';
$ Result = $ this-> query ($ SQL );
$ Info = array ();
If ($ result ){
Foreach ($ result as $ key => $ val ){
$ Info [trim ($ val ['field'])] = array (
'Name' => trim ($ val ['field']),
'Type' => $ val ['type'],
'Notnull '=> (bool) ($ val ['null1'] = 1), // 1 indicates not Null
'Default' => $ val ['default1'],
'Primary' => false,
'Autoinc' => false,
);
}
}
// Obtain the primary key
$ SQL = 'select B. rdb $ field_name as field_name from rdb $ relation_constraints a join rdb $ index_segments B on. rdb $ index_name = B. rdb $ index_name where. rdb $ constraint_type = \ 'Primary KEY \ 'and. rdb $ relation_name = UPPER (\''. $ tableName. '\')';
$ Rs_temp = $ this-> query ($ SQL );
Foreach ($ rs_temp as $ row ){
$ Info [trim ($ row ['field _ name'])] ['Primary '] = True;
}
Return $ info;
}
/**
* Obtain database table information
* @ Access public
*/
Public function getTables ($ dbName = ''){
$ SQL = 'select DISTINCT RDB $ RELATION_NAME FROM RDB $ RELATION_FIELDS WHERE RDB $ SYSTEM_FLAG = 0 ';
$ Result = $ this-> query ($ SQL );
$ Info = array ();
Foreach ($ result as $ key => $ val ){
$ Info [$ key] = trim (current ($ val ));
}
Return $ info;
}
/**
* SQL command security Filtering
* @ Access public
* @ Param string $ str SQL command
* @ Return string
*/
Public function escapeString ($ str ){
Return str_replace ("'", "'' ", $ str );
}
/**
* Limit
* @ Access public
* @ Param $ limit expression
* @ Return string
*/
Public function parseLimit ($ limit ){
$ LimitStr = '';
If (! Empty ($ limit )){
$ Limit = explode (',', $ limit );
If (count ($ limit)> 1 ){
$ LimitStr = 'first'. $ limit [1]. 'skip'. $ limit [0]. '';
} Else {
$ LimitStr = 'first'. $ limit [0]. '';
}
}
Return $ limitStr;
}
}
AD: truly free, domain name + VM + enterprise mailbox = 0 RMB