Skip to content

Commit

Permalink
Merge 3985736 into c1482f1
Browse files Browse the repository at this point in the history
  • Loading branch information
danrwalker committed May 15, 2017
2 parents c1482f1 + 3985736 commit 622e4cc
Show file tree
Hide file tree
Showing 4 changed files with 105 additions and 8 deletions.
36 changes: 36 additions & 0 deletions dist/twist/Core/Models/Database/Records.model.php
Original file line number Diff line number Diff line change
Expand Up @@ -180,6 +180,42 @@ public function count($mxdValue = null,$strField = null){
return $intOut;
}

/**
* Increment the value of a field by 1, setting the fourth parameter will increment by a custom number. The new value of the incremented field si returned.
* @param string|array $mxdValue Value(s) to filter by
* @param string $strField Field to be filtered
* @param string $strIncrementField Field to be incremented
* @param int $intIncrementStep Number to increment the field by
* @return null Value of the field after increment
*/
public function increment($mxdValue,$strField = 'id',$strIncrementField,$intIncrementStep = 1){

$intOut = null;

\Twist::Database()->query(sprintf("UPDATE `%s`.`%s` SET `%s` = `%s` + %d %s",
\Twist::Database()->escapeString($this->strDatabase),
\Twist::Database()->escapeString($this->strTable),
\Twist::Database()->escapeString($strIncrementField),
\Twist::Database()->escapeString($strIncrementField),
\Twist::Database()->escapeString($intIncrementStep),
$this->buildWhereClause($mxdValue,$strField)
));

$resResult = \Twist::Database()->query(sprintf("SELECT `%s` FROM `%s`.`%s`%s",
\Twist::Database()->escapeString($strIncrementField),
\Twist::Database()->escapeString($this->strDatabase),
\Twist::Database()->escapeString($this->strTable),
$this->buildWhereClause($mxdValue,$strField)
));

if($resResult->status() && $resResult->numberRows()){
$arrRecord = $resResult->row();
$intOut = $arrRecord[$strIncrementField];
}

return $intOut;
}

/**
* Get/Search data in the database, leaving all the parameters blank will return all records form the database. Set the parameters accordingly if you need to filter, order or limit.
* If the filter value contains a '%' it will be filtered as a LIKE, if the value contains an array it will be imploded and filtered as an IN otherwise filtering will be done as an EQUALS.
Expand Down
61 changes: 53 additions & 8 deletions dist/twist/Core/Utilities/Database.utility.php
Original file line number Diff line number Diff line change
Expand Up @@ -36,6 +36,7 @@ class Database extends Base{
protected $resResult = null;
protected $strConnectionKey = null;
protected $blConnectionAttempt = false;
protected $arrConnectionDetails = array();
protected $strDatabaseName = null;
protected $blNoDatabase = false;
protected $blDebugMode = false;
Expand Down Expand Up @@ -86,24 +87,38 @@ public function connect($strHost = null,$strUsername = null,$strPassword = null,
if(!is_null($strHost) || !is_null($strUsername) || !is_null($strPassword) || !is_null($strDatabaseName)){
if( !( !is_null($strHost) && !is_null($strUsername) && !is_null($strPassword) && !is_null($strDatabaseName) ) ){
throw new \Exception('Missing parameters passed into database connect');
}else{
//Store the connection details here, if custom details where used this will allow for reconnection if required
$this->arrConnectionDetails = array(
'host' => $strHost,
'username' => $strUsername,
'password' => $strPassword,
'database' => $strDatabaseName,
'protocol' => $strProtocol
);
}
}else{
$this->checkSettings(true);
$strHost = TWIST_DATABASE_HOST;
$strUsername = TWIST_DATABASE_USERNAME;
$strPassword = TWIST_DATABASE_PASSWORD;
$strDatabaseName = TWIST_DATABASE_NAME;

if(count($this->arrConnectionDetails) == 0){
$this->arrConnectionDetails = array(
'host' => TWIST_DATABASE_HOST,
'username' => TWIST_DATABASE_USERNAME,
'password' => TWIST_DATABASE_PASSWORD,
'database' => TWIST_DATABASE_NAME,
'protocol' => TWIST_DATABASE_PROTOCOL
);
}
}

$this->resLibrary = new $strLibraryClass();
$this->resLibrary->connect($strHost,$strUsername,$strPassword,$strDatabaseName);
$this->resLibrary->connect($this->arrConnectionDetails['host'],$this->arrConnectionDetails['username'],$this->arrConnectionDetails['password'],$this->arrConnectionDetails['database']);

//Set the parameter to say that the database has already been connected
$this->blConnectionAttempt = true;

if($this->connected()){
$this->strDatabaseName = $strDatabaseName;
$this->resLibrary->selectDatabase($strDatabaseName);
$this->setDatabase($this->arrConnectionDetails['database']);
$this->resLibrary->setCharset('UTF8');
$this->autoCommit(true);
}
Expand Down Expand Up @@ -210,6 +225,23 @@ public function mbSupport(){
return $blMultibyteSupport;
}

/**
* Select the default database for this connection to be using, all further queries will then be run on the newly selected database
* @param string $strDatabase Name of the database to be selected
* @return bool True indicates a successful database switch
*/
public function setDatabase($strDatabase){

if($this->resLibrary->selectDatabase($strDatabase)){

$this->arrConnectionDetails['database'] = $strDatabase;
$this->strDatabaseName = $strDatabase;
return true;
}

return false;
}

/**
* Run a fully formed SQL query on the database, optionally pass the query in as a raw sprintf() string "SELECT * FROM `table` WHERE `id` = %d" followed by all the parameters to fill the string
* All parameters are escaped before being entered into the sprintf(). A Database result object will be returned containing all the stats and results for the query.
Expand Down Expand Up @@ -368,8 +400,15 @@ public function importSQL($dirSQLFile,$strDatabaseName = null){
$dirSQLFile
);

$blOut = \Twist::Command()->execute($strCommand);
$arrResult = \Twist::Command()->execute($strCommand);
$blOut = $arrResult['status'];
}else{

if(!is_null($strDatabaseName)){
//Set the database for an inport
$this->resLibrary->selectDatabase($strDatabaseName);
}

//Run the import using the query function. May want to do some sanitation here?
$strSQLData = file_get_contents($dirSQLFile);
$arrQueries = explode(';',$strSQLData);
Expand All @@ -381,7 +420,13 @@ public function importSQL($dirSQLFile,$strDatabaseName = null){
$blAnyIssues = true;
}
}

$blOut = !$blAnyIssues;

if(!is_null($strDatabaseName)){
//Reset the database after the import
$this->resLibrary->selectDatabase($this->strDatabaseName);
}
}
}

Expand Down
15 changes: 15 additions & 0 deletions tests/Core/Utilities/DatabaseMySQLi.Test.php
Original file line number Diff line number Diff line change
Expand Up @@ -91,6 +91,21 @@ public function testCreateDelete(){
$this->assertEquals(0,count($arrResult2));
}

public function testImport(){

$blResult = \Twist::Database()->importSQL(TWIST_APP.'/Data/import.sql',TWIST_DATABASE_NAME);
$this->assertTrue($blResult);

$resRecord = \Twist::Database()->records('twist_settings')->get('SITE_AUTHOR','key',true);
$this->assertEquals('import-test',$resRecord['value']);
}

public function testSetDatabase(){

$blResult = \Twist::Database()->setDatabase('something-blah');
$this->assertTrue(!$blResult);
}

public function testFindCount(){

$intResult = \Twist::Database()->records('twist_settings')->count('SITE_%','key');
Expand Down
1 change: 1 addition & 0 deletions tests/app/Data/import.sql
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
UPDATE `twist_settings` SET `value` = 'import-test' WHERE `key` = 'SITE_AUTHOR';

0 comments on commit 622e4cc

Please sign in to comment.