From bad7e5b9c973bdee3a501a81530a7f379e558a18 Mon Sep 17 00:00:00 2001 From: Troy Kelly Date: Sun, 29 May 2016 18:32:44 +1000 Subject: [PATCH 1/3] Added support for socket Google App Engine uses Sockets for connectivity e.g. unix_socket=/cloudsql/appname:instancename --- src/Index.php | 105 ++++++++++++++++++++++++++++---------------------- 1 file changed, 58 insertions(+), 47 deletions(-) diff --git a/src/Index.php b/src/Index.php index 23a7701..a640541 100644 --- a/src/Index.php +++ b/src/Index.php @@ -44,17 +44,23 @@ class Index extends \Eden\Sql\Index * @var string|null $pass Database password */ protected $pass = null; - + + /** + * @var string|null $socket Socket connection + */ + protected $socket = null; + /** * Construct: Store connection information * - * @param *string $host Database host - * @param *string|null $name Database name - * @param *string|null $user Database user name - * @param string|null $pass Database password - * @param number|null $port Database port + * @param *string $host Database host + * @param *string|null $name Database name + * @param *string|null $user Database user name + * @param string|null $pass Database password + * @param number|null $port Database port + * @param string|null $socket Database socket */ - public function __construct($host, $name, $user, $pass = null, $port = null) + public function __construct($host, $name, $user, $pass = null, $port = null, $socket = null) { //argument test Argument::i() @@ -68,14 +74,17 @@ public function __construct($host, $name, $user, $pass = null, $port = null) ->test(4, 'string', 'null') //Argument 5 must be a number or null ->test(5, 'numeric', 'null'); - + //Argument 6 must be a string or null + ->test(6, 'string', 'null'); + $this->host = $host; $this->name = $name; $this->user = $user; $this->pass = $pass; $this->port = $port; + $this->socket = $socket; } - + /** * Returns the alter query builder * @@ -87,10 +96,10 @@ public function alter($name = null) { //Argument 1 must be a string or null Argument::i()->test(1, 'string', 'null'); - + return Alter::i($name); } - + /** * Returns the create query builder * @@ -102,10 +111,10 @@ public function create($name = null) { //Argument 1 must be a string or null Argument::i()->test(1, 'string', 'null'); - + return Create::i($name); } - + /** * Connects to the database * @@ -115,24 +124,26 @@ public function create($name = null) */ public function connect(array $options = array()) { - $host = $port = null; - + $host = null; + if (!is_null($this->host)) { $host = 'host='.$this->host.';'; if (!is_null($this->port)) { - $port = 'port='.$this->port.';'; + $host .= 'port='.$this->port.';'; } + } elseif (!is_null($this->socket)) { + $host = $this->socket.';'; } - - $connection = 'mysql:'.$host.$port.'dbname='.$this->name; - + + $connection = 'mysql:'.$host.'dbname='.$this->name; + $this->connection = new \PDO($connection, $this->user, $this->pass, $options); - + $this->trigger('mysql-connect'); - + return $this; } - + /** * Returns the Subselect query builder * @@ -145,10 +156,10 @@ public function subselect($parentQuery, $select = '*') { //Argument 2 must be a string Argument::i()->test(2, 'string'); - + return Subselect::i($parentQuery, $select); } - + /** * Returns the alter query builder * @@ -158,7 +169,7 @@ public function utility() { return Utility::i(); } - + /** * Returns the columns and attributes given the table name * @@ -171,9 +182,9 @@ public function getColumns($table, $filters = null) { //Argument 1 must be a string Argument::i()->test(1, 'string'); - + $query = $this->utility(); - + if (is_array($filters)) { foreach ($filters as $i => $filter) { //array('post_id=%s AND post_title IN %s', 123, array('asd')); @@ -182,11 +193,11 @@ public function getColumns($table, $filters = null) $filters[$i] = vsprintf($format, $filter); } } - + $query->showColumns($table, $filters); return $this->query($query, $this->getBinds()); } - + /** * Peturns the primary key name given the table * @@ -198,12 +209,12 @@ public function getPrimaryKey($table) { //Argument 1 must be a string Argument::i()->test(1, 'string'); - + $query = $this->utility(); $results = $this->getColumns($table, "`Key` = 'PRI'"); return isset($results[0]['Field']) ? $results[0]['Field'] : null; } - + /** * Returns the whole enitre schema and rows * of the current databse @@ -217,10 +228,10 @@ public function getSchema() foreach ($tables as $table) { $backup[] = $this->getBackup(); } - + return implode("\n\n", $backup); } - + /** * Returns a listing of tables in the DB * @@ -232,7 +243,7 @@ public function getTables($like = null) { //Argument 1 must be a string or null Argument::i()->test(1, 'string', 'null'); - + $query = $this->utility(); $like = $like ? $this->bind($like) : null; $results = $this->query($query->showTables($like), $q->getBinds()); @@ -243,10 +254,10 @@ public function getTables($like = null) break; } } - + return $newResults; } - + /** * Returns the whole enitre schema and rows * of the current table @@ -259,7 +270,7 @@ public function getTableSchema($table) { //Argument 1 must be a string Argument::i()->test(1, 'string'); - + $backup = array(); //get the schema $schema = $this->getColumns($table); @@ -270,15 +281,15 @@ public function getTableSchema($table) //first try to parse what we can from each field $fieldTypeArray = explode(' ', $field['Type']); $typeArray = explode('(', $fieldTypeArray[0]); - + $type = $typeArray[0]; $length = str_replace(')', '', $typeArray[1]); $attribute = isset($fieldTypeArray[1]) ? $fieldTypeArray[1] : null; - + $null = strtolower($field['Null']) == 'no' ? false : true; - + $increment = strtolower($field['Extra']) == 'auto_increment' ? true : false; - + //lets now add a field to our schema class $q->addField($field['Field'], array( 'type' => $type, @@ -287,7 +298,7 @@ public function getTableSchema($table) 'null' => $null, 'default' => $field['Default'], 'auto_increment' => $increment)); - + //set keys where found switch ($field['Key']) { case 'PRI': @@ -301,14 +312,14 @@ public function getTableSchema($table) break; } } - + //store the query but dont run it $backup[] = $query; } - + //get the rows $rows = $this->query($this->select->from($table)->getQuery()); - + if (count($rows)) { //lets build an insert query $query = $this->insert($table); @@ -317,11 +328,11 @@ public function getTableSchema($table) $query->set($key, $this->getBinds($value), $index); } } - + //store the query but dont run it $backup[] = $query->getQuery(true); } - + return implode("\n\n", $backup); } } From c78ce3c6440a06a6c6e53f6ba54380aa4d89618d Mon Sep 17 00:00:00 2001 From: Troy Kelly Date: Sun, 29 May 2016 21:12:22 +1000 Subject: [PATCH 2/3] Fixed incorrect semicolon --- src/Index.php | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/src/Index.php b/src/Index.php index a640541..af90e5b 100644 --- a/src/Index.php +++ b/src/Index.php @@ -73,7 +73,7 @@ public function __construct($host, $name, $user, $pass = null, $port = null, $so //Argument 4 must be a string or null ->test(4, 'string', 'null') //Argument 5 must be a number or null - ->test(5, 'numeric', 'null'); + ->test(5, 'numeric', 'null') //Argument 6 must be a string or null ->test(6, 'string', 'null'); From 76f21b3ae1d99739d055f9211dc9ba8287035c57 Mon Sep 17 00:00:00 2001 From: Troy Kelly Date: Sun, 29 May 2016 21:22:36 +1000 Subject: [PATCH 3/3] Updated readme --- README.md | 34 +++++++++++++++++----------------- 1 file changed, 17 insertions(+), 17 deletions(-) diff --git a/README.md b/README.md index f9c019a..cc4546b 100644 --- a/README.md +++ b/README.md @@ -30,7 +30,7 @@ The following documentation uses `eden()` in its example reference. Enabling thi Eden\Mysql\Index::i(); ``` -When using composer, there is not an easy way to access functions from packages. As a workaround, adding this constant in your code will allow `eden()` to be available after. +When using composer, there is not an easy way to access functions from packages. As a workaround, adding this constant in your code will allow `eden()` to be available after. ```php Eden::DECORATOR; @@ -54,7 +54,7 @@ eden()->inspect('Hello World'); **Figure 1. Database Connection Information** ```php -$database = eden('mysql', '[HOST]' ,'[DBNAME]', '[USER]', '[PASS]'); //instantiate +$database = eden('mysql', '[HOST]' ,'[DBNAME]', '[USER]', '[PASS]', '[SOCKET]'); //instantiate ``` Simply put, we first instantiate the class giving the host, database name, user name and password in the figure above. There are no other requirements and you can start using methods immediately. @@ -102,12 +102,12 @@ Figure 4. Data Manilpulation $settings = array( 'user_name' => 'Chris' 'user_email' => 'myemail@mail.com'); - + $filter[] = array('user_id=%s', 1); // inserts row into 'user' table $database->insertRow('user', $settings); -// updates rows in 'user' table where user_id is +// updates rows in 'user' table where user_id is $database->updateRows('user', $settings, $filter); // delete rows in 'user' table where user_id is 1 $database->deleteRows('user', $filter); @@ -123,7 +123,7 @@ Inserting data is pretty trivial. We included 2 ways to insert data. Like getRow ```php $settings = array('user_name' => 'Chris', 'user_email' => 'myemail@mail.com'); $database->insertRow('user', $settings); // inserts row into 'user' table - + $settings = array(); $settings[] = array('user_name' => 'Chris', 'user_email' => 'myemail@mail.com'); $settings[] = array('user_name' => 'Dan', 'user_email' => 'myemail2@mail.com'); @@ -243,7 +243,7 @@ When your happy with your query you can retrieve the results in 3 ways as descri ->getCollection() ``` -`Figure 10` shows three ways to get the results, the first way `getTotal()`, will retrieve the total number and does not consider pagination elements. `getRows()` will simply return a raw array. `getCollection()` will return you an object with the results for further manipulation. +`Figure 10` shows three ways to get the results, the first way `getTotal()`, will retrieve the total number and does not consider pagination elements. `getRows()` will simply return a raw array. `getCollection()` will return you an object with the results for further manipulation. ==== @@ -266,12 +266,12 @@ foreach($collection as $model) { echo $model->getUserName().' '; echo $model['user_email']; } - + //access as array echo $collection[0]['user_name']; //set as array -$collection[0]['user_email'] = 'my@email.com'; - +$collection[0]['user_email'] = 'my@email.com'; + $collection->save('user', $database); //save to 'user' table in database //only relavent columns will be saved //for all rows @@ -283,7 +283,7 @@ Some other utility methods not covered by th above examples are date formating a ```php //formats a date column -$collection->formatTime('post_created', 'F d, y g:ia'); +$collection->formatTime('post_created', 'F d, y g:ia'); //for each row, copy the value of post_user to the user_id column $collection->copy('post_user', 'user_id'); @@ -313,15 +313,15 @@ In *Eden*, we managed to loosely define models which takes off the restrictivene ```php $model->setUserName('Chris'); //set user name $model->getUserEmail(); // returns user email - + //$model->setAnyThing() // set or get any abstract key - + echo $model['user_name']; //access as array $model['user_email'] = 'my@email.com'; //set as array - + echo $model->user_name; //access as object $model->user_name = 'my@email.com'; //set as object - + $model->save('user', $database); //save to 'user' table in database //only relavent columns will be saved ``` @@ -340,7 +340,7 @@ $row = array( 'post_user' => 1, 'post_title' => 'My Post', 'post_detail' => 'This is my new article'); - + $db->model($row)->save('user')->save('post'); ``` @@ -397,8 +397,8 @@ Contributions to *Eden* are following the Github work flow. Please read up befor ##Setting up your machine with the Eden repository and your fork 1. Fork the repository -2. Fire up your local terminal create a new branch from the `v4` branch of your -fork with a branch name describing what your changes are. +2. Fire up your local terminal create a new branch from the `v4` branch of your +fork with a branch name describing what your changes are. Possible branch name types: - bugfix - feature