├── .travis.yml ├── AUTHORS ├── LICENSE ├── README.md ├── composer.json ├── phpcs.xml ├── phpunit.xml ├── src ├── Alter.php ├── Argument.php ├── Base.php ├── Create.php ├── Exception.php ├── Index.php └── Utility.php └── test ├── Collection.php ├── Index.php ├── Model.php ├── Search.php ├── assets └── unit.db └── bootstrap.php /.travis.yml: -------------------------------------------------------------------------------- 1 | language: php 2 | php: 3 | - 5.4 4 | install: 5 | - pear install pear/PHP_CodeSniffer 6 | - /home/travis/.phpenv/versions/5.5/bin/composer install 7 | - phpenv rehash 8 | script: 9 | - phpunit 10 | - phpcs 11 | -------------------------------------------------------------------------------- /AUTHORS: -------------------------------------------------------------------------------- 1 | Eden Core is written and maintained by Openovate Labs: 2 | 3 | Project Founder: 4 | 5 | - Christian Blanquera -------------------------------------------------------------------------------- /LICENSE: -------------------------------------------------------------------------------- 1 | Copyright (c) 2014-2016 Openovate Labs 2 | 3 | Permission is hereby granted, free of charge, to any person obtaining a copy 4 | of this software and associated documentation files (the "Software"), to deal 5 | in the Software without restriction, including without limitation the rights 6 | to use, copy, modify, merge, publish, distribute, sublicense, and/or sell 7 | copies of the Software, and to permit persons to whom the Software is furnished 8 | to do so, subject to the following conditions: 9 | 10 | The above copyright notice and this permission notice shall be included in all 11 | copies or substantial portions of the Software. 12 | 13 | THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR 14 | IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, 15 | FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE 16 | AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER 17 | LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, 18 | OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN 19 | THE SOFTWARE. -------------------------------------------------------------------------------- /README.md: -------------------------------------------------------------------------------- 1 | ![logo](http://eden.openovate.com/assets/images/cloud-social.png) Eden SQLite 2 | ==== 3 | [![Build Status](https://api.travis-ci.org/Eden-PHP/Sqlite.png)](https://travis-ci.org/Eden-PHP/Sqlite) 4 | ==== 5 | 6 | - [Install](#install) 7 | - [Introduction](#intro) 8 | - [Contributing](#contributing) 9 | 10 | ==== 11 | 12 | 13 | ## Install 14 | 15 | `composer install eden/sqlite` 16 | 17 | ==== 18 | 19 | 20 | ## Introduction 21 | 22 | *Eden's* SQLite objects are nothing short of spectacular! We take ORM in to a whole new level featuring search, collections and models, designed for novices and enthuisiasts alike. Before we go into those advanced topics, we should first go over the basic methods available. First off we need to add connection information to our database model shown in `Figure 1`. 23 | 24 | **Figure 1. Database Connection Information** 25 | 26 | ``` 27 | $database = eden('sqlite', '[SQL_FILE_PATH]'); //instantiate 28 | ``` 29 | 30 | Simply put, we instantiate the class giving the file path of the database on the server. There are no other requirements and you can start using methods immediately. 31 | 32 | > **Note** Eden's SQLite object will only connect when the first query is made. This strategy is called lazy loading. 33 | 34 | ==== 35 | 36 | 37 | ## Basic Querying 38 | 39 | On a very low level can call raw queries as in Figure 2. 40 | 41 | **Figure 2. Raw Query** 42 | 43 | ``` 44 | $database->query('SELECT * FROM user'); // returns results of raw queries 45 | ``` 46 | 47 | *Eden's* SQLite object uses PDO to communcate with your database. It's recommended you bind incoming variables contributed by the end user. Still on a low level, this can be acheived as in `Figure 3`. 48 | 49 | **Figure 3. Raw Binding** 50 | 51 | ``` 52 | $query = 'SELECT * FROM user WHERE user_name LIKE :user_name AND user_active = :user_active'; 53 | $bind = array(':user_name' => '%'.$userName.'%', ':user_active' => 1); 54 | 55 | $database->query($query, $bind); // returns results of raw queries 56 | ``` 57 | 58 | The above figure sets `$query` to a string with binded place holders `:user_name` and `:user_active`. `$bind` has the actual values these placeholders and should be replaced with duing execution of the query. We encourage this method because binding values prevents database injections. 59 | 60 | > **Note:** Binded variables must start with a colon(:). 61 | 62 | ==== 63 | 64 | 65 | ## Data Manipulation 66 | 67 | If you prefer the ORM way to save data Figure 4 provides several method examples on how to acheive this. 68 | 69 | Figure 4. Data Manilpulation 70 | 71 | ``` 72 | $settings = array( 73 | 'user_name' => 'Chris' 74 | 'user_email' => 'myemail@mail.com'); 75 | 76 | $filter[] = array('user_id=%s', 1); 77 | 78 | // inserts row into 'user' table 79 | $database->insertRow('user', $settings); 80 | // updates rows in 'user' table where user_id is 81 | $database->updateRows('user', $settings, $filter); 82 | // delete rows in 'user' table where user_id is 1 83 | $database->deleteRows('user', $filter); 84 | 85 | //updates data if there is a user_email with the value of anemail@gmail.com otherwise will insert 86 | $database->setRow('user', 'user_email', 'anemail@gmail.com', $settings); 87 | ``` 88 | 89 | Inserting data is pretty trivial. We included 2 ways to insert data. Like getRow(), there's no need to worry about binded data because Eden wil do this for you. Figure 4 shows the 2 kind of inserts mentioned. 90 | 91 | **Figure 4. Two ways to insert** 92 | 93 | ``` 94 | $settings = array('user_name' => 'Chris', 'user_email' => 'myemail@mail.com'); 95 | $database->insertRow('user', $settings); // inserts row into 'user' table 96 | 97 | $settings = array(); 98 | $settings[] = array('user_name' => 'Chris', 'user_email' => 'myemail@mail.com'); 99 | $settings[] = array('user_name' => 'Dan', 'user_email' => 'myemail2@mail.com'); 100 | $settings[] = array('user_name' => 'Clark', 'user_email' => 'myemail3@mail.com'); 101 | $database->insertRows('user', $settings); // inserts multiple rows into 'user' table 102 | ``` 103 | 104 | So obviously `insertRow()` should be used if you just want to insert one row. Inserting two or more rows at the same time, you should use `insertRows()`. This method expects an array of arrays, or an array table. 105 | 106 | > **Note:** A common error found amongst programmers using Eden, is simply using `insertRows()` instead of `insertRow()`. 107 | 108 | > **Note:** Using models and collections, you don't really need to worry about this method because it's covered in the `save()` method in a collection or model object. We'll go over models and collections later in this section. 109 | 110 | Updating is about as easy as inserting. There's only one method you need to know. 111 | 112 | **Figure 5. Updating** 113 | 114 | ``` 115 | $settings = array('user_name' => 'Chris', 'user_email' => 'myemail@mail.com'); 116 | $filter[] = array('user_id=%s', 1); 117 | $database->updateRows('user', $settings, $filter); // inserts row into 'user' table 118 | ``` 119 | 120 | A common scenario is when you need to insert if a column value is not found and update if it is. We added an extra method called `setRow()` to simply to save you some lines of redundancy. 121 | 122 | **Figure 6. Insert or update** 123 | 124 | ``` 125 | $settings = array('user_name' => 'Chris2', 'user_email' => 'myemail@mail.com'); 126 | $database->setRow('user', 'user_email', 'myemail@mail.com', $settings); 127 | ``` 128 | 129 | `Figure 6` basically says, in user table, if `myemail@mail.com` exists in the `user_email` column, then update that row. If not then insert. Removing data is simple enough as well in *Eden*. 130 | 131 | **Figure 7. Remove** 132 | 133 | ``` 134 | $filter[] = array('user_id=%s', 1); 135 | $database->deleteRows('user', $filter); // delete rows in 'user' table where user_id is 1 136 | ``` 137 | 138 | ==== 139 | 140 | 141 | ## Searching 142 | 143 | A better way to build complex queries is with using Eden's search object. An overview example can be found in `Figure 8`. 144 | 145 | **Figure 8. SQLite Search** 146 | 147 | ``` 148 | $database 149 | ->search('user') 150 | ->setColumns('*') 151 | ->innerJoinOn('group', 'group_owner=user_id') 152 | ->leftJoinUsing('friends', 'user_id') 153 | ->filterByUserName('Chris') 154 | ->addFilter("user_last LIKE '%s%%'", 'Brown') 155 | ->sortByUserId('ASC') 156 | ->addSort('user_last', 'DESC') 157 | ->setRange(25) 158 | ->setStart(75) 159 | ->getRows(); 160 | ``` 161 | 162 | In the figure above there's a few methods being powered with magic, but we'll just start going down the line. First off, to instantiate the search object you simply need to call `search()` passing the name of the table as the argument. Secondly we call `setColumns()`. This call is optional, but if used, can either accept an array of columns or an argument separated list of columns, ie. `setColumns('user_id', 'user_name')`. Next, `innerJoinOn()` is the new way we accept joins. There are eight methods dedicated to different kinds of joins. 163 | 164 | **Kinds of Join methods** 165 | 166 | ``` 167 | innerJoinOn() 168 | innerJoinUsing() 169 | leftJoinOn() 170 | leftJoinUsing() 171 | rightJoinOn() 172 | rightJoinUsing() 173 | outerJoinOn() 174 | outerJoinUsing() 175 | ``` 176 | 177 | No matter what methods you choose from above there are two arguments you need to add. The first argument is the name of the table you would like to join and the second one is the how they relate to each other. 178 | 179 | The first magic powered method is called `filterByUserName()`. There is no actual method called `filterByUserName()` in the SQLite class. Instead when this function is called it will parse out the name of the method and recognize that UserName is the name of a column and convert that into `addFilter('user_name=%s', 'Chris')` as in `Figure 8`. 180 | 181 | `addFilter()` generally accepts two arguments. The first argument is the filter clause. If you notice in our filter example in `Figure 8` we use %s to delimit a binded value. You can have as many binded values per filter as you like. The following arguments need to include the binded values in order of when they occur in the filter clause. 182 | 183 | The second magic powered mehod is called `sortByUserId('ASC')`.There is no actual method called `sortByUserId('ASC')` in the SQLite class. Instead when this function is called it will parse out the name of the method and recognize that UserId is the name of a column and convert that into `addSort('user_id', 'ASC')` as in `Figure 8`. 184 | 185 | There are three kinds of pagination methods also available 186 | 187 | **Pagination Methods** 188 | 189 | ``` 190 | setRange(75) 191 | setStart(25) 192 | setPage(1) 193 | ``` 194 | 195 | It's important if you are going to use `setPage(1)` to call `setRange(75)` first because the underlying function simply calculates the start index based on the range. Two other methods that are not covered by `Figure 8` are the ability to group and to set the table to something else. 196 | 197 | **Figure 9. Other Useful methods** 198 | 199 | ``` 200 | ->setTable('user') 201 | ->setGroup('user_active') 202 | ``` 203 | 204 | ### Getting Results 205 | 206 | When your happy with your query you can retrieve the results in 3 ways as described in Figure 0. 207 | 208 | **Figure 10. Retrieving Results** 209 | 210 | ``` 211 | ->getTotal() 212 | ->getRows() 213 | ->getCollection() 214 | ``` 215 | 216 | `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. 217 | 218 | ==== 219 | 220 | 221 | ## Collections 222 | 223 | Collections do exactly the same thing as models except it manipulates multiple models instead. Collections can be iterable and access as arrays as well. Collections only hold model objects so if you wanted to use your own extended model, you would need to call `setModel('Your_Model')`. 224 | 225 | **Figure 11. SQLite Collections** 226 | 227 | ``` 228 | //set user name for all rows 229 | $collection->setUserName('Chris'); 230 | 231 | // set or get any abstract key for all rows 232 | $collection->setAnyThing() 233 | 234 | //collections are iterable 235 | foreach($collection as $model) { 236 | echo $model->getUserName().' '; 237 | echo $model['user_email']; 238 | } 239 | 240 | //access as array 241 | echo $collection[0]['user_name']; 242 | //set as array 243 | $collection[0]['user_email'] = 'my@email.com'; 244 | 245 | $collection->save('user', $database); //save to 'user' table in database 246 | //only relavent columns will be saved 247 | //for all rows 248 | ``` 249 | 250 | Some other utility methods not covered by th above examples are date formating and copying from one column to another. `Figure 12`, show how we would go about doing these things. 251 | 252 | **Figure 12. Utility methods** 253 | 254 | ``` 255 | //formats a date column 256 | $collection->formatTime('post_created', 'F d, y g:ia'); 257 | 258 | //for each row, copy the value of post_user to the user_id column 259 | $collection->copy('post_user', 'user_id'); 260 | 261 | //remove the row with the index of 1, reindexes all the rows 262 | $collection->cut(1); 263 | 264 | //returns the number of rows 265 | $collection->count(); 266 | 267 | //adds a new row 268 | $collection->add(array('post_title' => 'Hi')); 269 | 270 | //returns a table array (no objects) 271 | $collection->get(); 272 | ``` 273 | 274 | ==== 275 | 276 | 277 | ## Models 278 | 279 | In *Eden*, we managed to loosely define models which takes off the restrictiveness of a normal ORM and adds scalability as an end result. First off, what we did was define a generic, yet powerful model class that can be extended, but also can be used as is. Our model class is already powerful enough to solve for a lot of use cases, you might not need to extend it. We played around with the concept of "loosely defined" and here's what we came up with. 280 | 281 | **Figure 13. Database Model (Extends Array)** 282 | 283 | ``` 284 | $model->setUserName('Chris'); //set user name 285 | $model->getUserEmail(); // returns user email 286 | 287 | //$model->setAnyThing() // set or get any abstract key 288 | 289 | echo $model['user_name']; //access as array 290 | $model['user_email'] = 'my@email.com'; //set as array 291 | 292 | echo $model->user_name; //access as object 293 | $model->user_name = 'my@email.com'; //set as object 294 | 295 | $model->save('user', $database); //save to 'user' table in database 296 | //only relavent columns will be saved 297 | ``` 298 | 299 | So model properties can be accesed by method, object or array. The preference we leave up to you. With our model, you can put extra key values in the object, even if it has nothing to do with the intended database table. When you call `save()`, this is when you need to specify the table your saving to. This method is really powerful, in that it will first check to see what columns exist in the table then compares it with your model. It will only save columns that have the matching column name in your object. Lastly it will auto determine whether if we should insert or update that row. 300 | 301 | A common example is when you have an array table that comprises of joined data. In Eden you can leave that array as is then call `save()` for each table as in `Figure 14`. 302 | 303 | **Figure 14. Two tables** 304 | 305 | ``` 306 | $row = array( 307 | 'user_id' => 1, 308 | 'user_name' => 'Chris', 309 | 'user_email' => 'my@email.com', 310 | 'post_user' => 1, 311 | 'post_title' => 'My Post', 312 | 'post_detail' => 'This is my new article'); 313 | 314 | $db->model($row)->save('user')->save('post'); 315 | ``` 316 | 317 | > **Note:** You can also save to different databases as in `save('post', $db2)` 318 | 319 | ==== 320 | 321 | 322 | ## Putting it all together 323 | 324 | So a common scenario would be retrieving data, manipulating the results and sending back to the database. Let's see with *Eden's* search, collection and model objects how we can acheive this. 325 | 326 | **Figure 15. The Coolest Thing Ever!** 327 | 328 | ``` 329 | //load database 330 | eden('sqlite', SQLITE_FILE) 331 | //search user table 332 | ->search('user') 333 | //WHERE user_gender = $_get['gender'] 334 | ->filterByUserGender($_GET['gender']) 335 | //ORDER BY user_id 336 | ->sortByUserId('ASC') 337 | //LIMIT 75, 25 338 | ->setStart(75)->setRange(25) 339 | //get a collection object 340 | ->getCollection() 341 | //sets all users to active 342 | ->setUserActive(1) 343 | //Set a new column post_title 344 | ->setPostTitle('A '.$_GET['gender'].'\'s Post') 345 | //Set a new column post_detail 346 | ->setPostDetail('Content is King') 347 | //Copy the contents of user_id to a new column post_user 348 | ->copy('user_id', 'post_user') 349 | //Set a new column post_created 350 | ->setPostCreated(time()) 351 | ->formatTime('post_created', 'Y-m-d H:i:s') 352 | //save to user table 353 | ->save('user') 354 | //save to post table 355 | ->save('post'); 356 | ``` 357 | 358 | If you look at our PostGreSQL or even our MySQL documentation. You'll realize that it's the same documentation as this one with the exception of small changes basic query section. We normalized all SQL database objects to use the exact same thing to reduce the learning curve. 359 | 360 | ==== 361 | 362 | 363 | #Contributing to Eden 364 | 365 | Contributions to *Eden* are following the Github work flow. Please read up before contributing. 366 | 367 | ##Setting up your machine with the Eden repository and your fork 368 | 369 | 1. Fork the repository 370 | 2. Fire up your local terminal create a new branch from the `v4` branch of your 371 | fork with a branch name describing what your changes are. 372 | Possible branch name types: 373 | - bugfix 374 | - feature 375 | - improvement 376 | 3. Make your changes. Always make sure to sign-off (-s) on all commits made (git commit -s -m "Commit message") 377 | 378 | ##Making pull requests 379 | 380 | 1. Please ensure to run `phpunit` before making a pull request. 381 | 2. Push your code to your remote forked version. 382 | 3. Go back to your forked version on GitHub and submit a pull request. 383 | 4. An Eden developer will review your code and merge it in when it has been classified as suitable. -------------------------------------------------------------------------------- /composer.json: -------------------------------------------------------------------------------- 1 | { 2 | "name": "eden/sqlite", 3 | "type": "library", 4 | "description": "Eden SQLite Search, Collection, Model ORM componen", 5 | "keywords": ["eden", "library"], 6 | "homepage": "http://eden-php.com", 7 | "license": "MIT", 8 | "authors": [ 9 | { 10 | "name": "Christian Blanquera", 11 | "email": "cblanquera@openovate.com" 12 | } 13 | ], 14 | "require": { 15 | "php": ">=5.4.1", 16 | "eden/core": "4.*", 17 | "eden/string": "4.*", 18 | "eden/array": "4.*", 19 | "eden/model": "4.*", 20 | "eden/collection": "4.*", 21 | "eden/sql": "4.*" 22 | }, 23 | "autoload": { 24 | "psr-4": { "Eden\\Sqlite\\": "src/" } 25 | } 26 | } 27 | -------------------------------------------------------------------------------- /phpcs.xml: -------------------------------------------------------------------------------- 1 | 2 | 3 | The coding standard for PHP_CodeSniffer itself. 4 | 5 | src 6 | 7 | 8 | 9 | 10 | 11 | -------------------------------------------------------------------------------- /phpunit.xml: -------------------------------------------------------------------------------- 1 | 2 | 3 | 14 | 15 | 16 | test/Collection.php 17 | test/Index.php 18 | test/Model.php 19 | test/Search.php 20 | 21 | 22 | 23 | 24 | 25 | ./ 26 | 27 | 28 | -------------------------------------------------------------------------------- /src/Alter.php: -------------------------------------------------------------------------------- 1 | 2 | /** 3 | * This file is part of the Eden PHP Library. 4 | * (c) 2014-2016 Openovate Labs 5 | * 6 | * Copyright and license information can be found at LICENSE.txt 7 | * distributed with this package. 8 | */ 9 | 10 | namespace Eden\Sqlite; 11 | 12 | /** 13 | * Generates alter query string syntax 14 | * 15 | * @vendor Eden 16 | * @package Sqlite 17 | * @author Christian Blanquera 18 | * @standard PSR-2 19 | */ 20 | class Alter extends \Eden\Sql\Query 21 | { 22 | /** 23 | * @var string|null $name Name of table 24 | */ 25 | protected $name = null; 26 | 27 | /** 28 | * @var array $changeFields List of fields to change 29 | */ 30 | protected $changeFields = array(); 31 | 32 | /** 33 | * @var array $addFields List of fields to add 34 | */ 35 | protected $addFields = array(); 36 | 37 | /** 38 | * @var array $removeFields List of fields to remove 39 | */ 40 | protected $removeFields = array(); 41 | 42 | /** 43 | * @var array $addKeys List of keys to add 44 | */ 45 | protected $addKeys = array(); 46 | 47 | /** 48 | * @var array $removeKeys List of keys to remove 49 | */ 50 | protected $removeKeys = array(); 51 | 52 | /** 53 | * @var array $addUniqueKeys List of unique keys to add 54 | */ 55 | protected $addUniqueKeys = array(); 56 | 57 | /** 58 | * @var array $removeUniqueKeys List of unique keys to remove 59 | */ 60 | protected $removeUniqueKeys = array(); 61 | 62 | /** 63 | * @var array $addPrimaryKeys List of primary keys to add 64 | */ 65 | protected $addPrimaryKeys = array(); 66 | 67 | /** 68 | * @var array $removePrimaryKeys List of primary keys to remove 69 | */ 70 | protected $removePrimaryKeys = array(); 71 | 72 | /** 73 | * Construct: Set the table, if any 74 | * 75 | * @param string|null 76 | */ 77 | public function __construct($name = null) 78 | { 79 | if (is_string($name)) { 80 | $this->setName($name); 81 | } 82 | } 83 | 84 | /** 85 | * Adds a field in the table 86 | * 87 | * @param *string $name Column name 88 | * @param *array $attributes Column attributes 89 | * 90 | * @return Eden\Sqlite\Alter 91 | */ 92 | public function addField($name, array $attributes) 93 | { 94 | //Argument 1 must be a string 95 | Argument::i()->test(1, 'string'); 96 | 97 | $this->addFields[$name] = $attributes; 98 | return $this; 99 | } 100 | 101 | /** 102 | * Adds an index key 103 | * 104 | * @param *string $name Name of key 105 | * 106 | * @return Eden\Sqlite\Alter 107 | */ 108 | public function addForeignKey($name, $table, $key) 109 | { 110 | //argument test 111 | Argument::i() 112 | //Argument 1 must be a string 113 | ->test(1, 'string') 114 | //Argument 2 must be a string 115 | ->test(2, 'string') 116 | //Argument 3 must be a string 117 | ->test(3, 'string'); 118 | 119 | $this->addKeys[$name] = array($table, $key); 120 | return $this; 121 | } 122 | 123 | /** 124 | * Adds a unique key 125 | * 126 | * @param *string $name Name of key 127 | * 128 | * @return Eden\Sqlite\Alter 129 | */ 130 | public function addUniqueKey($name) 131 | { 132 | //Argument 1 must be a string 133 | Argument::i()->test(1, 'string'); 134 | 135 | $this->addUniqueKeys[] = '"'.$name.'"'; 136 | return $this; 137 | } 138 | 139 | /** 140 | * Changes attributes of the table given 141 | * the field name 142 | * 143 | * @param *string $name Column name 144 | * @param *array $attributes Column attributes 145 | * 146 | * @return Eden\Sqlite\Alter 147 | */ 148 | public function changeField($name, array $attributes) 149 | { 150 | //Argument 1 must be a string 151 | Argument::i()->test(1, 'string'); 152 | 153 | $this->changeFields[$name] = $attributes; 154 | return $this; 155 | } 156 | 157 | /** 158 | * Returns the string version of the query 159 | * 160 | * @param bool 161 | * 162 | * @return string 163 | */ 164 | public function getQuery($unbind = false) 165 | { 166 | $fields = array(); 167 | $table = '"'.$this->name.'"'; 168 | 169 | foreach ($this->removeFields as $name) { 170 | $fields[] = 'DROP "'.$name.'"'; 171 | } 172 | 173 | foreach ($this->addFields as $name => $attr) { 174 | $field = array('ADD "'.$name.'"'); 175 | if (isset($attr['type'])) { 176 | $field[] = isset($attr['length']) ? 177 | $attr['type'] . '('.$attr['length'].')' : 178 | $attr['type']; 179 | } 180 | 181 | if (isset($attr['attribute'])) { 182 | $field[] = $attr['attribute']; 183 | } 184 | 185 | if (isset($attr['null'])) { 186 | if ($attr['null'] == false) { 187 | $field[] = 'NOT NULL'; 188 | } else { 189 | $field[] = 'DEFAULT NULL'; 190 | } 191 | } 192 | 193 | if (isset($attr['default'])&& $attr['default'] !== false) { 194 | if (!isset($attr['null']) || $attr['null'] == false) { 195 | if (is_string($attr['default'])) { 196 | $field[] = 'DEFAULT \''.$attr['default'] . '\''; 197 | } else if (is_numeric($attr['default'])) { 198 | $field[] = 'DEFAULT '.$attr['default']; 199 | } 200 | } 201 | } 202 | 203 | $fields[] = implode(' ', $field); 204 | } 205 | 206 | foreach ($this->changeFields as $name => $attr) { 207 | $field = array('CHANGE "'.$name.'" "'.$name.'"'); 208 | 209 | if (isset($attr['name'])) { 210 | $field = array('CHANGE "'.$name.'" "'.$attr['name'].'"'); 211 | } 212 | 213 | if (isset($attr['type'])) { 214 | $field[] = isset($attr['length']) ? 215 | $attr['type'] . '('.$attr['length'].')' : 216 | $attr['type']; 217 | } 218 | 219 | if (isset($attr['attribute'])) { 220 | $field[] = $attr['attribute']; 221 | } 222 | 223 | if (isset($attr['null'])) { 224 | if ($attr['null'] == false) { 225 | $field[] = 'NOT NULL'; 226 | } else { 227 | $field[] = 'DEFAULT NULL'; 228 | } 229 | } 230 | 231 | if (isset($attr['default'])&& $attr['default'] !== false) { 232 | if (!isset($attr['null']) || $attr['null'] == false) { 233 | if (is_string($attr['default'])) { 234 | $field[] = 'DEFAULT \''.$attr['default'] . '\''; 235 | } else if (is_numeric($attr['default'])) { 236 | $field[] = 'DEFAULT '.$attr['default']; 237 | } 238 | } 239 | } 240 | 241 | $fields[] = implode(' ', $field); 242 | } 243 | 244 | foreach ($this->removeKeys as $key) { 245 | $fields[] = 'DROP FOREIGN KEY "'.$key.'"'; 246 | } 247 | 248 | foreach ($this->keys as $key => $value) { 249 | $fields[] = 'ADD FOREIGN KEY "'. $key .'" REFERENCES '.$value[0].'('.$value[1].')'; 250 | } 251 | 252 | foreach ($this->removeUniqueKeys as $key) { 253 | $fields[] = 'DROP UNIQUE "'.$key.'"'; 254 | } 255 | 256 | if (!empty($this->addUniqueKeys)) { 257 | $fields[] = 'ADD UNIQUE ('.implode(', ', $this->addUniqueKeys).')'; 258 | } 259 | 260 | $fields = implode(", \n", $fields); 261 | 262 | return sprintf( 263 | 'ALTER TABLE %s %s;', 264 | $table, 265 | $fields 266 | ); 267 | } 268 | 269 | /** 270 | * Removes a field 271 | * 272 | * @param *string $name Name of field 273 | * 274 | * @return Eden\Sqlite\Alter 275 | */ 276 | public function removeField($name) 277 | { 278 | //Argument 1 must be a string 279 | Argument::i()->test(1, 'string'); 280 | 281 | $this->removeFields[] = $name; 282 | return $this; 283 | } 284 | 285 | /** 286 | * Removes an index key 287 | * 288 | * @param *string $name Name of key 289 | * 290 | * @return Eden\Sqlite\Alter 291 | */ 292 | public function removeForeignKey($name) 293 | { 294 | //Argument 1 must be a string 295 | Argument::i()->test(1, 'string'); 296 | 297 | $this->removeKeys[] = $name; 298 | return $this; 299 | } 300 | 301 | /** 302 | * Removes a unique key 303 | * 304 | * @param *string $name Name of key 305 | * 306 | * @return Eden\Sqlite\Alter 307 | */ 308 | public function removeUniqueKey($name) 309 | { 310 | //Argument 1 must be a string 311 | Argument::i()->test(1, 'string'); 312 | 313 | $this->removeUniqueKeys[] = $name; 314 | return $this; 315 | } 316 | 317 | /** 318 | * Sets the name of the table you wish to create 319 | * 320 | * @param *string $name Name of table 321 | * 322 | * @return Eden\Sqlite\Alter 323 | */ 324 | public function setName($name) 325 | { 326 | //Argument 1 must be a string 327 | Argument::i()->test(1, 'string'); 328 | 329 | $this->name = $name; 330 | return $this; 331 | } 332 | } 333 | -------------------------------------------------------------------------------- /src/Argument.php: -------------------------------------------------------------------------------- 1 | 2 | /** 3 | * This file is part of the Eden PHP Library. 4 | * (c) 2014-2016 Openovate Labs 5 | * 6 | * Copyright and license information can be found at LICENSE.txt 7 | * distributed with this package. 8 | */ 9 | 10 | namespace Eden\Sqlite; 11 | 12 | /** 13 | * Argument Class 14 | * 15 | * @vendor Eden 16 | * @package Sqlite 17 | * @author Christian Blanquera 18 | * @standard PSR-2 19 | */ 20 | class Argument extends \Eden\Core\Argument 21 | { 22 | } 23 | -------------------------------------------------------------------------------- /src/Base.php: -------------------------------------------------------------------------------- 1 | 2 | /** 3 | * This file is part of the Eden PHP Library. 4 | * (c) 2014-2016 Openovate Labs 5 | * 6 | * Copyright and license information can be found at LICENSE.txt 7 | * distributed with this package. 8 | */ 9 | 10 | namespace Eden\Sqlite; 11 | 12 | /** 13 | * Base Class 14 | * 15 | * @vendor Eden 16 | * @package Sqlite 17 | * @author Christian Blanquera 18 | * @standard PSR-2 19 | */ 20 | class Base extends \Eden\Core\Base 21 | { 22 | } 23 | -------------------------------------------------------------------------------- /src/Create.php: -------------------------------------------------------------------------------- 1 | 2 | /** 3 | * This file is part of the Eden PHP Library. 4 | * (c) 2014-2016 Openovate Labs 5 | * 6 | * Copyright and license information can be found at LICENSE.txt 7 | * distributed with this package. 8 | */ 9 | 10 | namespace Eden\Sqlite; 11 | 12 | /** 13 | * Generates create table query string syntax 14 | * 15 | * @vendor Eden 16 | * @package Sqlite 17 | * @author Christian Blanquera 18 | * @standard PSR-2 19 | */ 20 | class Create extends \Eden\Sql\Query 21 | { 22 | /** 23 | * @var string|null $name Name of table 24 | */ 25 | protected $name = null; 26 | 27 | /** 28 | * @var string|null $comments Table comments 29 | */ 30 | protected $comments = null; 31 | 32 | /** 33 | * @var array $fields List of fields 34 | */ 35 | protected $fields = array(); 36 | 37 | /** 38 | * @var array $keys List of key indexes 39 | */ 40 | protected $keys = array(); 41 | 42 | /** 43 | * @var array $uniqueKeys List of unique keys 44 | */ 45 | protected $uniqueKeys = array(); 46 | 47 | /** 48 | * @var array $primaryKeys List of primary keys 49 | */ 50 | protected $primaryKeys = array(); 51 | 52 | /** 53 | * Construct: Set the table, if any 54 | * 55 | * @param string|null $name Name of table 56 | */ 57 | public function __construct($name = null) 58 | { 59 | if (is_string($name)) { 60 | $this->setName($name); 61 | } 62 | } 63 | 64 | /** 65 | * Adds a field in the table 66 | * 67 | * @param *string $name Column name 68 | * @param *array $attributes Column attributes 69 | * 70 | * @return Eden\Sqlite\Create 71 | */ 72 | public function addField($name, array $attributes) 73 | { 74 | //Argument 1 must be a string 75 | Argument::i()->test(1, 'string'); 76 | 77 | $this->fields[$name] = $attributes; 78 | return $this; 79 | } 80 | 81 | /** 82 | * Adds an index key 83 | * 84 | * @param *string $name Name of column 85 | * @param *string $table Name of foreign table 86 | * @param *string $key Name of key 87 | * 88 | * @return Eden\Sqlite\Create 89 | */ 90 | public function addForeignKey($name, $table, $key) 91 | { 92 | //argument test 93 | Argument::i() 94 | ->test(1, 'string') //Argument 1 must be a string 95 | ->test(2, 'string') //Argument 2 must be a string 96 | ->test(3, 'string'); //Argument 3 must be a string 97 | 98 | $this->keys[$name] = array($table, $key); 99 | return $this; 100 | } 101 | 102 | /** 103 | * Adds a unique key 104 | * 105 | * @param *string $name Name of key 106 | * @param *array $fields List of key fields 107 | * 108 | * @return Eden\Sqlite\Create 109 | */ 110 | public function addUniqueKey($name, array $fields) 111 | { 112 | //Argument 1 must be a string 113 | Argument::i()->test(1, 'string'); 114 | 115 | $this->uniqueKeys[$name] = $fields; 116 | return $this; 117 | } 118 | 119 | /** 120 | * Returns the string version of the query 121 | * 122 | * @param bool $unbind Whether to unbind variables 123 | * 124 | * @return string 125 | */ 126 | public function getQuery($unbind = false) 127 | { 128 | $table = '"'.$this->name.'"'; 129 | 130 | $fields = array(); 131 | foreach ($this->fields as $name => $attr) { 132 | $field = array('"'.$name.'"'); 133 | if (isset($attr['type'])) { 134 | $field[] = isset($attr['length']) ? 135 | $attr['type'] . '('.$attr['length'].')' : 136 | $attr['type']; 137 | } 138 | 139 | if (isset($attr['primary'])) { 140 | $field[] = 'PRIMARY KEY'; 141 | } 142 | 143 | if (isset($attr['attribute'])) { 144 | $field[] = $attr['attribute']; 145 | } 146 | 147 | if (isset($attr['null'])) { 148 | if ($attr['null'] == false) { 149 | $field[] = 'NOT NULL'; 150 | } else { 151 | $field[] = 'DEFAULT NULL'; 152 | } 153 | } 154 | 155 | if (isset($attr['default'])&& $attr['default'] !== false) { 156 | if (!isset($attr['null']) || $attr['null'] == false) { 157 | if (is_string($attr['default'])) { 158 | $field[] = 'DEFAULT \''.$attr['default'] . '\''; 159 | } else if (is_numeric($attr['default'])) { 160 | $field[] = 'DEFAULT '.$attr['default']; 161 | } 162 | } 163 | } 164 | 165 | $fields[] = implode(' ', $field); 166 | } 167 | 168 | $fields = !empty($fields) ? implode(', ', $fields) : ''; 169 | 170 | $uniques = array(); 171 | foreach ($this->uniqueKeys as $key => $value) { 172 | $uniques[] = 'UNIQUE "'. $key .'" ("'.implode('", "', $value).'")'; 173 | } 174 | 175 | $uniques = !empty($uniques) ? ', ' . implode(", \n", $uniques) : ''; 176 | 177 | $keys = array(); 178 | foreach ($this->keys as $key => $value) { 179 | $keys[] = 'FOREIGN KEY "'. $key .'" REFERENCES '.$value[0].'('.$value[1].')'; 180 | } 181 | 182 | $keys = !empty($keys) ? ', ' . implode(", \n", $keys) : ''; 183 | 184 | return sprintf( 185 | 'CREATE TABLE %s (%s%s%s)', 186 | $table, 187 | $fields, 188 | $unique, 189 | $keys 190 | ); 191 | } 192 | 193 | /** 194 | * Sets comments 195 | * 196 | * @param *string $comments Table comments 197 | * 198 | * @return Eden\Sqlite\Create 199 | */ 200 | public function setComments($comments) 201 | { 202 | //Argument 1 must be a string 203 | Argument::i()->test(1, 'string'); 204 | 205 | $this->comments = $comments; 206 | return $this; 207 | } 208 | 209 | /** 210 | * Sets a list of fields to the table 211 | * 212 | * @param *array $fields List of fields 213 | * 214 | * @return Eden\Sqlite\Create 215 | */ 216 | public function setFields(array $fields) 217 | { 218 | $this->fields = $fields; 219 | return $this; 220 | } 221 | 222 | /** 223 | * Sets a list of keys to the table 224 | * 225 | * @param array $keys A list of foreign keys 226 | * 227 | * @return Eden\Sqlite\Create 228 | */ 229 | public function setForiegnKeys(array $keys) 230 | { 231 | $this->keys = $keys; 232 | return $this; 233 | } 234 | 235 | /** 236 | * Sets the name of the table you wish to create 237 | * 238 | * @param *string $name Table name 239 | * 240 | * @return Eden\Sqlite\Create 241 | */ 242 | public function setName($name) 243 | { 244 | //Argument 1 must be a string 245 | Argument::i()->test(1, 'string'); 246 | 247 | $this->name = $name; 248 | return $this; 249 | } 250 | 251 | /** 252 | * Sets a list of unique keys to the table 253 | * 254 | * @param *array $uniqueKeys List of unique keys 255 | * 256 | * @return Eden\Sqlite\Create 257 | */ 258 | public function setUniqueKeys(array $uniqueKeys) 259 | { 260 | $this->uniqueKeys = $uniqueKeys; 261 | return $this; 262 | } 263 | } 264 | -------------------------------------------------------------------------------- /src/Exception.php: -------------------------------------------------------------------------------- 1 | 2 | /** 3 | * This file is part of the Eden PHP Library. 4 | * (c) 2014-2016 Openovate Labs 5 | * 6 | * Copyright and license information can be found at LICENSE.txt 7 | * distributed with this package. 8 | */ 9 | 10 | namespace Eden\Sqlite; 11 | 12 | /** 13 | * Exception Class 14 | * 15 | * @vendor Eden 16 | * @package Sqlite 17 | * @author Christian Blanquera 18 | * @standard PSR-2 19 | */ 20 | class Exception extends \Eden\Sql\Exception 21 | { 22 | } 23 | -------------------------------------------------------------------------------- /src/Index.php: -------------------------------------------------------------------------------- 1 | 2 | /** 3 | * This file is part of the Eden PHP Library. 4 | * (c) 2014-2016 Openovate Labs 5 | * 6 | * Copyright and license information can be found at LICENSE.txt 7 | * distributed with this package. 8 | */ 9 | 10 | namespace Eden\Sqlite; 11 | 12 | /** 13 | * Abstractly defines a layout of available methods to 14 | * connect to and query a SQLite database. This class also 15 | * lays out query building methods that auto renders a 16 | * valid query the specific database will understand without 17 | * actually needing to know the query language. Extending 18 | * all SQL classes, comes coupled with loosely defined 19 | * searching, collections and models. 20 | * 21 | * @vendor Eden 22 | * @package Sqlite 23 | * @author Christian Blanquera 24 | * @standard PSR-2 25 | */ 26 | class Index extends \Eden\Sql\Index 27 | { 28 | /** 29 | * @var string $path Sqlite file path 30 | */ 31 | protected $path = null; 32 | 33 | /** 34 | * Construct: Store connection information 35 | * 36 | * @param *string $path Sqlite file path 37 | */ 38 | public function __construct($path) 39 | { 40 | //argument test 41 | Argument::i()->test(1, 'string'); 42 | $this->path = $path; 43 | } 44 | 45 | /** 46 | * Returns the alter query builder 47 | * 48 | * @param *string $name Name of table 49 | * 50 | * @return Eden\Sqlite\Alter 51 | */ 52 | public function alter($name = null) 53 | { 54 | //Argument 1 must be a string or null 55 | Argument::i()->test(1, 'string', 'null'); 56 | 57 | return Alter::i($name); 58 | } 59 | 60 | /** 61 | * Connects to the database 62 | * 63 | * @param array $options The connection options 64 | * 65 | * @return Eden\Sqlite\Index 66 | */ 67 | public function connect(array $options = array()) 68 | { 69 | $this->connection = new \PDO('sqlite:'.$this->path); 70 | $this->connection->setAttribute(\PDO::ATTR_ERRMODE, \PDO::ERRMODE_EXCEPTION); 71 | $this->trigger('connect'); 72 | 73 | return $this; 74 | } 75 | 76 | /** 77 | * Returns the create query builder 78 | * 79 | * @param string $name Name of table 80 | * 81 | * @return Eden\Sqlite\Create 82 | */ 83 | public function create($name = null) 84 | { 85 | //Argument 1 must be a string or null 86 | Argument::i()->test(1, 'string', 'null'); 87 | 88 | return Create::i($name); 89 | } 90 | 91 | /** 92 | * Returns the columns and attributes given the table name 93 | * 94 | * @param *string $table The name of the table 95 | * 96 | * @return array|false 97 | */ 98 | public function getColumns($table) 99 | { 100 | //Argument 1 must be a string 101 | Argument::i()->test(1, 'string'); 102 | 103 | $query = $this->utility()->showColumns($table); 104 | $results = $this->query($query, $this->getBinds()); 105 | 106 | $columns = array(); 107 | foreach ($results as $column) { 108 | $key = null; 109 | if ($column['pk'] == 1) { 110 | $key = 'PRI'; 111 | } 112 | 113 | $columns[] = array( 114 | 'Field' => $column['name'], 115 | 'Type' => $column['type'], 116 | 'Default' => $column['dflt_value'], 117 | 'Null' => $column['notnull'] != 1, 118 | 'Key' => $key); 119 | } 120 | 121 | return $columns; 122 | } 123 | 124 | /** 125 | * Peturns the primary key name given the table 126 | * 127 | * @param *string $table The table name 128 | * 129 | * @return string 130 | */ 131 | public function getPrimaryKey($table) 132 | { 133 | //Argument 1 must be a string 134 | Argument::i()->test(1, 'string'); 135 | 136 | $query = $this->utility(); 137 | $results = $this->getColumns($table, "`Key` = 'PRI'"); 138 | return isset($results[0]['Field']) ? $results[0]['Field'] : null; 139 | } 140 | 141 | /** 142 | * Returns the whole enitre schema and rows 143 | * of the current databse 144 | * 145 | * @return string 146 | */ 147 | public function getSchema() 148 | { 149 | $backup = array(); 150 | $tables = $this->getTables(); 151 | foreach ($tables as $table) { 152 | $backup[] = $this->getBackup(); 153 | } 154 | 155 | return implode("\n\n", $backup); 156 | } 157 | 158 | /** 159 | * Returns the whole enitre schema and rows 160 | * of the current table 161 | * 162 | * @param *string $table The table name 163 | * 164 | * @return string 165 | */ 166 | public function getTableSchema($table) 167 | { 168 | //Argument 1 must be a string 169 | Argument::i()->test(1, 'string'); 170 | 171 | $backup = array(); 172 | //get the schema 173 | $schema = $this->getColumns($table); 174 | if (count($schema)) { 175 | //lets rebuild this schema 176 | $query = $this->create()->setName($table); 177 | foreach ($schema as $field) { 178 | //first try to parse what we can from each field 179 | $fieldTypeArray = explode(' ', $field['Type']); 180 | $typeArray = explode('(', $fieldTypeArray[0]); 181 | 182 | $type = $typeArray[0]; 183 | $length = str_replace(')', '', $typeArray[1]); 184 | $attribute = isset($fieldTypeArray[1]) ? $fieldTypeArray[1] : null; 185 | 186 | $null = strtolower($field['Null']) == 'no' ? false : true; 187 | 188 | $increment = strtolower($field['Extra']) == 'auto_increment' ? true : false; 189 | 190 | //lets now add a field to our schema class 191 | $q->addField($field['Field'], array( 192 | 'type' => $type, 193 | 'length' => $length, 194 | 'attribute' => $attribute, 195 | 'null' => $null, 196 | 'default' => $field['Default'], 197 | 'auto_increment' => $increment)); 198 | 199 | //set keys where found 200 | switch ($field['Key']) { 201 | case 'PRI': 202 | $query->addPrimaryKey($field['Field']); 203 | break; 204 | case 'UNI': 205 | $query->addUniqueKey($field['Field'], array($field['Field'])); 206 | break; 207 | case 'MUL': 208 | $query->addKey($field['Field'], array($field['Field'])); 209 | break; 210 | } 211 | } 212 | 213 | //store the query but dont run it 214 | $backup[] = $query; 215 | } 216 | 217 | //get the rows 218 | $rows = $this->query($this->select->from($table)->getQuery()); 219 | if (count($rows)) { 220 | //lets build an insert query 221 | $query = $this->insert($table); 222 | foreach ($rows as $index => $row) { 223 | foreach ($row as $key => $value) { 224 | $query->set($key, $this->getBinds($value), $index); 225 | } 226 | } 227 | 228 | //store the query but dont run it 229 | $backup[] = $query->getQuery(true); 230 | } 231 | 232 | return implode("\n\n", $backup); 233 | } 234 | 235 | /** 236 | * Returns a listing of tables in the DB 237 | * 238 | * @param string|null $like The like pattern 239 | * 240 | * @return attay|false 241 | */ 242 | public function getTables($like = null) 243 | { 244 | //Argument 1 must be a string or null 245 | Argument::i()->test(1, 'string', 'null'); 246 | 247 | $query = $this->utility(); 248 | $like = $like ? $this->bind($like) : null; 249 | $results = $this->query($query->showTables($like), $q->getBinds()); 250 | $newResults = array(); 251 | foreach ($results as $result) { 252 | foreach ($result as $key => $value) { 253 | $newResults[] = $value; 254 | break; 255 | } 256 | } 257 | 258 | return $newResults; 259 | } 260 | 261 | /** 262 | * Inserts multiple rows into a table 263 | * 264 | * @param *string $table Table name 265 | * @param array $setting Key/value 2D array matching table columns 266 | * @param bool|array $bind Whether to compute with binded variables 267 | * 268 | * @return Eden\Sqlite\Index 269 | */ 270 | public function insertRows($table, array $settings, $bind = true) 271 | { 272 | //argument test 273 | Argument::i() 274 | //Argument 1 must be a string 275 | ->test(1, 'string') 276 | //Argument 3 must be an array or bool 277 | ->test(3, 'array', 'bool'); 278 | 279 | //this is an array of arrays 280 | foreach ($settings as $index => $setting) { 281 | //SQLite no available multi insert 282 | //there's work arounds, but no performance gain 283 | $this->insertRow($table, $setting, $bind); 284 | } 285 | 286 | return $this; 287 | } 288 | 289 | /** 290 | * Returns the select query builder 291 | * 292 | * @param string|array $select Column list 293 | * 294 | * @return Eden\Sql\Select 295 | */ 296 | public function select($select = 'ROWID,*') 297 | { 298 | //Argument 1 must be a string or array 299 | Argument::i()->test(1, 'string', 'array'); 300 | 301 | return \Eden\Sql\Select::i($select); 302 | } 303 | 304 | /** 305 | * Returns the alter query builder 306 | * 307 | * @return Eden\Sqlite\Utility 308 | */ 309 | public function utility() 310 | { 311 | return Utility::i(); 312 | } 313 | } 314 | -------------------------------------------------------------------------------- /src/Utility.php: -------------------------------------------------------------------------------- 1 | 2 | /** 3 | * This file is part of the Eden PHP Library. 4 | * (c) 2014-2016 Openovate Labs 5 | * 6 | * Copyright and license information can be found at LICENSE.txt 7 | * distributed with this package. 8 | */ 9 | 10 | namespace Eden\Sqlite; 11 | 12 | /** 13 | * Generates utility query strings 14 | * 15 | * @vendor Eden 16 | * @package Sqlite 17 | * @author Christian Blanquera 18 | * @standard PSR-2 19 | */ 20 | class Utility extends \Eden\Sql\Query 21 | { 22 | /** 23 | * @var string|null $query The query string 24 | */ 25 | protected $query = null; 26 | 27 | /** 28 | * Query for dropping a table 29 | * 30 | * @param *string $table The name of the table 31 | * 32 | * @return Eden\Sqlite\Utility 33 | */ 34 | public function dropTable($table) 35 | { 36 | //Argument 1 must be a string 37 | Argument::i()->test(1, 'string'); 38 | 39 | $this->query = 'DROP TABLE "' . $table .'"'; 40 | return $this; 41 | } 42 | 43 | /** 44 | * Returns the string version of the query 45 | * 46 | * @return string 47 | */ 48 | public function getQuery() 49 | { 50 | return $this->query.';'; 51 | } 52 | 53 | /** 54 | * Query for renaming a table 55 | * 56 | * @param *string $table The name of the table 57 | * @param *string $name The new name of the table 58 | * 59 | * @return Eden\Sqlite\Utility 60 | */ 61 | public function renameTable($table, $name) 62 | { 63 | //Argument 1 must be a string, 2 must be string 64 | Argument::i()->test(1, 'string')->test(2, 'string'); 65 | 66 | $this->query = 'RENAME TABLE "' . $table . '" TO "' . $name . '"'; 67 | return $this; 68 | } 69 | 70 | /** 71 | * Query for showing all columns of a table 72 | * 73 | * @param *string $table The name of the table 74 | * 75 | * @return Eden\Sqlite\Utility 76 | */ 77 | public function showColumns($table) 78 | { 79 | //Argument 1 must be a string 80 | Argument::i()->test(1, 'string'); 81 | 82 | $this->query = 'PRAGMA table_info('.$table.')'; 83 | return $this; 84 | } 85 | 86 | /** 87 | * Query for showing all tables 88 | * 89 | * @param string|null $like The like pattern 90 | * 91 | * @return Eden\Sqlite\Utility 92 | */ 93 | public function showTables() 94 | { 95 | $this->query = 'SELECT * FROM dbname.sqlite_master WHERE type=\'table\''; 96 | return $this; 97 | } 98 | 99 | /** 100 | * Query for truncating a table 101 | * 102 | * @param *string $table The name of the table 103 | * 104 | * @return Eden\Sqlite\Utility 105 | */ 106 | public function truncate($table) 107 | { 108 | //Argument 1 must be a string 109 | Argument::i()->test(1, 'string'); 110 | 111 | $this->query = 'TRUNCATE "' . $table .'"'; 112 | return $this; 113 | } 114 | } 115 | -------------------------------------------------------------------------------- /test/Collection.php: -------------------------------------------------------------------------------- 1 | 2 | /** 3 | * This file is part of the Eden PHP Library. 4 | * (c) 2014-2016 Openovate Labs 5 | * 6 | * Copyright and license information can be found at LICENSE.txt 7 | * distributed with this package. 8 | */ 9 | 10 | class EdenSqliteTestSqliteCollectionTest extends PHPUnit_Framework_TestCase 11 | { 12 | public static $database; 13 | 14 | public function setUp() { 15 | date_default_timezone_set('GMT'); 16 | self::$database = eden('sqlite', realpath(__DIR__.'/assets').'/unit.db'); 17 | } 18 | 19 | /* FACTORY METHODS */ 20 | public function testAlter() 21 | { 22 | 23 | } 24 | } -------------------------------------------------------------------------------- /test/Index.php: -------------------------------------------------------------------------------- 1 | 2 | /** 3 | * This file is part of the Eden PHP Library. 4 | * (c) 2014-2016 Openovate Labs 5 | * 6 | * Copyright and license information can be found at LICENSE.txt 7 | * distributed with this package. 8 | */ 9 | 10 | class EdenSqliteTestSqliteFactoryTest extends PHPUnit_Framework_TestCase 11 | { 12 | public static $database; 13 | 14 | public function setUp() { 15 | date_default_timezone_set('GMT'); 16 | self::$database = eden('sqlite', realpath(__DIR__.'/assets').'/unit.db'); 17 | 18 | /*self::$database->query('CREATE TABLE "unit_post" ( 19 | "post_id" INTEGER PRIMARY KEY AUTOINCREMENT NOT NULL, 20 | "post_slug" VARCHAR NOT NULL UNIQUE, "post_title" VARCHAR, 21 | "post_detail" TEXT, "post_active" SMALLINT NOT NULL DEFAULT 1, 22 | "post_type" VARCHAR NOT NULL DEFAULT post, 23 | "post_flag" SMALLINT NOT NULL DEFAULT 0, 24 | "post_visibility" VARCHAR NOT NULL DEFAULT public, 25 | "post_status" VARCHAR NOT NULL DEFAULT published, 26 | "post_published" DATE, 27 | "post_created" DATETIME NOT NULL, 28 | "post_updated" DATETIME NOT NULL);');*/ 29 | 30 | /* Schema 31 | CREATE TABLE "unit_post" ( 32 | "post_id" INTEGER PRIMARY KEY AUTOINCREMENT NOT NULL, 33 | "post_slug" VARCHAR NOT NULL UNIQUE, "post_title" VARCHAR, 34 | "post_detail" TEXT, "post_active" SMALLINT NOT NULL DEFAULT 1, 35 | "post_type" VARCHAR NOT NULL DEFAULT post, 36 | "post_flag" SMALLINT NOT NULL DEFAULT 0, 37 | "post_visibility" VARCHAR NOT NULL DEFAULT public, 38 | "post_status" VARCHAR NOT NULL DEFAULT published, 39 | "post_published" DATE, 40 | "post_created" DATETIME NOT NULL, 41 | "post_updated" DATETIME NOT NULL); */ 42 | } 43 | 44 | /* FACTORY METHODS */ 45 | public function testAlter() 46 | { 47 | $query = self::$database->alter(); 48 | 49 | $this->assertInstanceOf('Eden\\Sqlite\\Alter', $query); 50 | } 51 | 52 | public function testCollection() 53 | { 54 | $collection = self::$database->collection(); 55 | 56 | $this->assertInstanceOf('Eden\\Sql\\Collection', $collection); 57 | } 58 | 59 | public function testCreate() 60 | { 61 | $query = self::$database->create(); 62 | 63 | $this->assertInstanceOf('Eden\\Sqlite\\Create', $query); 64 | } 65 | 66 | public function testDelete() 67 | { 68 | $query = self::$database->delete(); 69 | 70 | $this->assertInstanceOf('Eden\\Sql\\Delete', $query); 71 | } 72 | 73 | public function testInsert() 74 | { 75 | $query = self::$database->insert(); 76 | 77 | $this->assertInstanceOf('Eden\\Sql\\Insert', $query); 78 | } 79 | 80 | public function testModel() 81 | { 82 | $query = self::$database->model(); 83 | 84 | $this->assertInstanceOf('Eden\\Sql\\Model', $query); 85 | } 86 | 87 | public function testSearch() 88 | { 89 | $search = self::$database->search(); 90 | 91 | $this->assertInstanceOf('Eden\\Sql\\Search', $search); 92 | } 93 | 94 | public function testSelect() 95 | { 96 | $query = self::$database->select(); 97 | 98 | $this->assertInstanceOf('Eden\\Sql\\Select', $query); 99 | } 100 | 101 | public function testUpdate() 102 | { 103 | $query = self::$database->update(); 104 | 105 | $this->assertInstanceOf('Eden\\Sql\\Update', $query); 106 | } 107 | 108 | public function testUtility() 109 | { 110 | $query = self::$database->utility(); 111 | 112 | $this->assertInstanceOf('Eden\\Sqlite\\Utility', $query); 113 | } 114 | 115 | /* CRUD METHODS */ 116 | public function testInsertRow() { 117 | $total = self::$database->search('unit_post')->getTotal(); 118 | 119 | self::$database->insertRow('unit_post', array( 120 | 'post_slug' => 'unit-test-1', 121 | 'post_title' => 'Unit Test 1', 122 | 'post_detail' => 'Unit Test Detail 1', 123 | 'post_published' => date('Y-m-d'), 124 | 'post_created' => date('Y-m-d H:i:s'), 125 | 'post_updated' => date('Y-m-d H:i:s'))); 126 | 127 | $id = self::$database->getLastInsertedId(); 128 | 129 | $now = self::$database->search('unit_post')->getTotal(); 130 | 131 | $this->assertTrue($id > 0); 132 | $this->assertEquals($total+1, $now); 133 | } 134 | 135 | public function testGetRow() { 136 | $row = self::$database->getRow('unit_post', 'post_slug', 'unit-test-1'); 137 | $this->assertEquals('Unit Test 1', $row['post_title']); 138 | } 139 | 140 | public function testUpdateRows() { 141 | self::$database->updateRows('unit_post', array( 142 | 'post_title' => 'Unit Test 2', 143 | 'post_updated' => date('Y-m-d H:i:s')), 144 | array('post_slug=%s', 'unit-test-1')); 145 | 146 | $row = self::$database->getRow('unit_post', 'post_slug', 'unit-test-1'); 147 | 148 | $this->assertEquals('Unit Test 2', $row['post_title']); 149 | } 150 | 151 | public function testInsertRows() { 152 | $total = self::$database->search('unit_post')->getTotal(); 153 | 154 | self::$database->insertRows('unit_post', array( 155 | array( 156 | 'post_slug' => 'unit-test-2', 157 | 'post_title' => 'Unit Test 2', 158 | 'post_detail' => 'Unit Test Detail 2', 159 | 'post_published' => date('Y-m-d'), 160 | 'post_created' => date('Y-m-d H:i:s'), 161 | 'post_updated' => date('Y-m-d H:i:s')), 162 | array( 163 | 'post_slug' => 'unit-test-3', 164 | 'post_title' => 'Unit Test 3', 165 | 'post_detail' => 'Unit Test Detail 3', 166 | 'post_published' => date('Y-m-d'), 167 | 'post_created' => date('Y-m-d H:i:s'), 168 | 'post_updated' => date('Y-m-d H:i:s')) 169 | )); 170 | 171 | $now = self::$database->search('unit_post')->getTotal(); 172 | 173 | $this->assertEquals($total+2, $now); 174 | } 175 | 176 | public function testSetRow() { 177 | $total = self::$database->search('unit_post')->getTotal(); 178 | 179 | self::$database->setRow('unit_post', 'post_slug', 'unit-test-4', array( 180 | 'post_slug' => 'unit-test-4', 181 | 'post_title' => 'Unit Test 4', 182 | 'post_detail' => 'Unit Test Detail 4', 183 | 'post_published' => date('Y-m-d'), 184 | 'post_created' => date('Y-m-d H:i:s'), 185 | 'post_updated' => date('Y-m-d H:i:s'))); 186 | 187 | $now = self::$database->search('unit_post')->getTotal(); 188 | 189 | $this->assertEquals($total+1, $now); 190 | 191 | self::$database->setRow('unit_post', 'post_slug', 'unit-test-4', array( 192 | 'post_slug' => 'unit-test-4', 193 | 'post_title' => 'Unit Test 5', 194 | 'post_detail' => 'Unit Test Detail 4', 195 | 'post_published' => date('Y-m-d'), 196 | 'post_created' => date('Y-m-d H:i:s'), 197 | 'post_updated' => date('Y-m-d H:i:s'))); 198 | 199 | $now = self::$database->search('unit_post')->getTotal(); 200 | 201 | $this->assertEquals($total+1, $now); 202 | 203 | $row = self::$database->getRow('unit_post', 'post_slug', 'unit-test-4'); 204 | 205 | $this->assertEquals('Unit Test 5', $row['post_title']); 206 | } 207 | 208 | /* Search Collection Models */ 209 | public function testGetModel() { 210 | $model1 = self::$database->getModel('unit_post', 'post_slug', 'unit-test-1'); 211 | $model2 = self::$database->getModel('unit_post', 'post_slug', 'doesnt exist'); 212 | 213 | $this->assertInstanceOf('Eden\\Sql\\Model', $model1); 214 | $this->assertEquals('Unit Test 2', $model1['post_title']); 215 | $this->assertNull($model2); 216 | } 217 | 218 | public function testDeleteRows() { 219 | $total = self::$database->search('unit_post')->getTotal(); 220 | 221 | self::$database->deleteRows('unit_post', array('post_slug LIKE %s', 'unit-test-%')); 222 | 223 | $now = self::$database->search('unit_post')->getTotal(); 224 | 225 | $this->assertEquals($total-4, $now); 226 | } 227 | } 228 | -------------------------------------------------------------------------------- /test/Model.php: -------------------------------------------------------------------------------- 1 | 2 | /** 3 | * This file is part of the Eden PHP Library. 4 | * (c) 2014-2016 Openovate Labs 5 | * 6 | * Copyright and license information can be found at LICENSE.txt 7 | * distributed with this package. 8 | */ 9 | 10 | class EdenSqliteTestSqliteModelTest extends PHPUnit_Framework_TestCase 11 | { 12 | public static $database; 13 | 14 | public function setUp() { 15 | date_default_timezone_set('GMT'); 16 | self::$database = eden('sqlite', realpath(__DIR__.'/assets').'/unit.db'); 17 | } 18 | 19 | /* FACTORY METHODS */ 20 | public function testAlter() 21 | { 22 | 23 | } 24 | } -------------------------------------------------------------------------------- /test/Search.php: -------------------------------------------------------------------------------- 1 | 2 | /** 3 | * This file is part of the Eden PHP Library. 4 | * (c) 2014-2016 Openovate Labs 5 | * 6 | * Copyright and license information can be found at LICENSE.txt 7 | * distributed with this package. 8 | */ 9 | 10 | class EdenSqliteTestSqliteSearchTest extends PHPUnit_Framework_TestCase 11 | { 12 | public static $database; 13 | 14 | public function setUp() { 15 | date_default_timezone_set('GMT'); 16 | self::$database = eden('sqlite', realpath(__DIR__.'/assets').'/unit.db'); 17 | } 18 | 19 | /* FACTORY METHODS */ 20 | public function testSearch() 21 | { 22 | self::$database->model(array( 23 | 'post_slug' => 'unit-test-1', 24 | 'post_title' => 'Unit Test 1', 25 | 'post_detail' => 'Unit Test Detail 1', 26 | 'post_published' => date('Y-m-d'), 27 | 'post_created' => date('Y-m-d H:i:s'), 28 | 'post_updated' => date('Y-m-d H:i:s'))) 29 | ->save('unit_post'); 30 | 31 | self::$database->model(array( 32 | 'post_slug' => 'unit-test-2', 33 | 'post_title' => 'Unit Test 2', 34 | 'post_detail' => 'Unit Test Detail 2', 35 | 'post_published' => date('Y-m-d'), 36 | 'post_created' => date('Y-m-d H:i:s'), 37 | 'post_updated' => date('Y-m-d H:i:s'))) 38 | ->save('unit_post'); 39 | 40 | self::$database->model(array( 41 | 'post_slug' => 'unit-test-3', 42 | 'post_title' => 'Unit Test 3', 43 | 'post_detail' => 'Unit Test Detail 3', 44 | 'post_published' => date('Y-m-d'), 45 | 'post_created' => date('Y-m-d H:i:s'), 46 | 'post_updated' => date('Y-m-d H:i:s'))) 47 | ->save('unit_post'); 48 | 49 | $collection = self::$database 50 | ->search('unit_post') 51 | ->filterByPostActive(1) 52 | ->sortByPostId('DESC') 53 | ->getCollection() 54 | ->setPostTitle('Unit Test X') 55 | ->save(); 56 | 57 | $this->assertSame('Unit Test X', $collection[0]['post_title']); 58 | $this->assertSame('Unit Test X', $collection[1]['post_title']); 59 | $this->assertSame('Unit Test X', $collection[2]['post_title']); 60 | 61 | $row = self::$database->getRow('unit_post', 'post_title', 'Unit Test X'); 62 | 63 | $this->assertTrue(!empty($row)); 64 | } 65 | } -------------------------------------------------------------------------------- /test/assets/unit.db: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/Eden-PHP/Sqlite/c4ca0909993f866537b07a6a201a0cfeb4f363b1/test/assets/unit.db -------------------------------------------------------------------------------- /test/bootstrap.php: -------------------------------------------------------------------------------- 1 | 2 | /** 3 | * This file is part of the Eden PHP Library. 4 | * (c) 2014-2016 Openovate Labs 5 | * 6 | * Copyright and license information can be found at LICENSE.txt 7 | * distributed with this package. 8 | */ 9 | ob_start(); 10 | if(file_exists(__DIR__.'/../../../autoload.php')) { 11 | require_once __DIR__.'/../../../autoload.php'; 12 | } else { 13 | require_once __DIR__.'/../vendor/autoload.php'; 14 | } 15 | 16 | 17 | if(file_exists(__DIR__.'/../../core/src/Control.php')) { 18 | require_once __DIR__.'/../../core/src/Control.php'; 19 | } else { 20 | require_once __DIR__.'/../vendor/eden/core/src/Control.php'; 21 | } 22 | 23 | --------------------------------------------------------------------------------