Skip to content

Commit 04411d3

Browse files
committed
Attogram Database v1.1.0 - setCreateTables() via string or array
1 parent 372a683 commit 04411d3

File tree

7 files changed

+137
-41
lines changed

7 files changed

+137
-41
lines changed

.gitignore

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,3 @@
11
.idea/
22
vendor/
3-
examples/test.sqlite
3+
examples/test.*.sqlite

README.md

Lines changed: 39 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -1,8 +1,8 @@
11
# Attogram Database
22

3-
ALPHA RELEASE
3+
_BETA RELEASE_
44

5-
PHP access to SQLite databases
5+
PHP access to SQLite databases.
66

77
[![Maintainability](https://api.codeclimate.com/v1/badges/473e68db98ac442429c1/maintainability)](https://codeclimate.com/github/attogram/database/maintainability)
88
[![Build Status](https://travis-ci.org/attogram/database.svg?branch=master)](https://travis-ci.org/attogram/database)
@@ -13,25 +13,54 @@ PHP access to SQLite databases
1313
composer require attogram/database
1414
```
1515

16-
## Example
16+
## Examples
1717

18+
one table:
1819
```php
19-
<?php
2020
declare(strict_types = 1);
2121

2222
use Attogram\Database\Database;
2323

2424
require '../vendor/autoload.php';
2525

2626
$database = new Database();
27+
$database->setDatabaseFile('./test.one.sqlite');
28+
$database->setCreateTables("CREATE TABLE 'one' ('foo' TEXT)");
2729

28-
$database->setDatabaseFile('./test.sqlite');
30+
try {
31+
$database->raw("INSERT INTO one ('foo') VALUES (CURRENT_TIMESTAMP)");
32+
$arrayResults = $database->query("SELECT * FROM 'one'");
33+
print_r($arrayResults);
34+
} catch (Throwable $error) {
35+
print 'ERROR: ' . $error->getMessage();
36+
}
37+
```
2938

30-
$database->setCreateTables("CREATE TABLE 'foo' ('bar' TEXT)");
39+
two tables:
40+
```php
41+
declare(strict_types = 1);
3142

32-
$database->raw("INSERT INTO foo ('bar') VALUES ('baz')");
43+
use Attogram\Database\Database;
3344

34-
$arrayResults = $database->query("SELECT * FROM 'foo'");
45+
require '../vendor/autoload.php';
3546

36-
print_r($arrayResults);
37-
```
47+
$database = new Database();
48+
$database->setDatabaseFile('./test.two.sqlite');
49+
50+
$tables = [
51+
"CREATE TABLE 'one' ('foo' TEXT)",
52+
"CREATE TABLE 'two' ('bar' TEXT)",
53+
];
54+
$database->setCreateTables($tables);
55+
56+
try {
57+
$database->raw("INSERT INTO one ('foo') VALUES (CURRENT_TIMESTAMP)");
58+
$database->raw("INSERT INTO two ('bar') VALUES (CURRENT_TIMESTAMP)");
59+
$arrayResults = $database->query("SELECT * FROM 'one'");
60+
print_r($arrayResults);
61+
$arrayResults = $database->query("SELECT * FROM 'two'");
62+
print_r($arrayResults);
63+
} catch (Throwable $error) {
64+
print 'ERROR: ' . $error->getMessage();
65+
}
66+
```

composer.json

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
{
22
"name": "attogram/database",
3-
"description": "PHP access to SQLite database",
3+
"description": "PHP access to SQLite databases.",
44
"type": "project",
55
"license": "MIT",
66
"homepage": "https://github.com/attogram/database",

examples/one.table.php

Lines changed: 24 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,24 @@
1+
<?php
2+
/**
3+
* Attogram Database
4+
* @see https://github.com/attogram/database
5+
*
6+
* Example: one table
7+
*/
8+
declare(strict_types = 1);
9+
10+
use Attogram\Database\Database;
11+
12+
require '../vendor/autoload.php';
13+
14+
$database = new Database();
15+
$database->setDatabaseFile('./test.one.sqlite');
16+
$database->setCreateTables("CREATE TABLE 'one' ('foo' TEXT)");
17+
18+
try {
19+
$database->raw("INSERT INTO one ('foo') VALUES (CURRENT_TIMESTAMP)");
20+
$arrayResults = $database->query("SELECT * FROM 'one'");
21+
print_r($arrayResults);
22+
} catch (Throwable $error) {
23+
print 'ERROR: ' . $error->getMessage();
24+
}

examples/simple.php

Lines changed: 0 additions & 18 deletions
This file was deleted.

examples/two.tables.php

Lines changed: 33 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,33 @@
1+
<?php
2+
/**
3+
* Attogram Database
4+
* @see https://github.com/attogram/database
5+
*
6+
* Example: two tables
7+
*/
8+
declare(strict_types = 1);
9+
10+
use Attogram\Database\Database;
11+
12+
require '../vendor/autoload.php';
13+
14+
$database = new Database();
15+
$database->setDatabaseFile('./test.two.sqlite');
16+
17+
$tables = [
18+
"CREATE TABLE 'one' ('foo' TEXT)",
19+
"CREATE TABLE 'two' ('bar' TEXT)",
20+
];
21+
$database->setCreateTables($tables);
22+
23+
try {
24+
$database->raw("INSERT INTO one ('foo') VALUES (CURRENT_TIMESTAMP)");
25+
$database->raw("INSERT INTO two ('bar') VALUES (CURRENT_TIMESTAMP)");
26+
$arrayResults = $database->query("SELECT * FROM 'one'");
27+
print_r($arrayResults);
28+
$arrayResults = $database->query("SELECT * FROM 'two'");
29+
print_r($arrayResults);
30+
} catch (Throwable $error) {
31+
print 'ERROR: ' . $error->getMessage();
32+
}
33+

src/Database.php

Lines changed: 39 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,8 @@
11
<?php
2+
/**
3+
* Attogram Database
4+
* @see https://github.com/attogram/database
5+
*/
26
declare(strict_types = 1);
37

48
namespace Attogram\Database;
@@ -15,16 +19,16 @@
1519
class Database
1620
{
1721
/** @var string */
18-
const VERSION = '1.0.0-pre.4';
22+
const VERSION = '1.1.0';
1923

2024
/** @var bool */
2125
private $connected = false;
2226

23-
/** @var string */
24-
private $createTables;
27+
/** @var array */
28+
private $createTables = [];
2529

2630
/** @var string */
27-
private $databaseFile;
31+
private $databaseFile = 'database.sqlite';
2832

2933
/** @var PDO */
3034
private $pdo;
@@ -38,11 +42,20 @@ public function setDatabaseFile(string $databaseFile)
3842
}
3943

4044
/**
41-
* @param string $createTables
45+
* @param string|array $createTables
4246
*/
43-
public function setCreateTables(string $createTables)
47+
public function setCreateTables($createTables)
4448
{
45-
$this->createTables = $createTables;
49+
if (empty($createTables)) {
50+
return;
51+
}
52+
if (is_string($createTables)) {
53+
$this->createTables[] = $createTables;
54+
return;
55+
}
56+
if (is_array($createTables)) {
57+
$this->createTables = array_merge($this->createTables, $createTables);
58+
}
4659
}
4760

4861
/**
@@ -70,19 +83,29 @@ public function connect()
7083
$this->pdo = new PDO('sqlite:'. $this->databaseFile);
7184
$this->connected = true;
7285
if ($doCreateTables && !empty($this->createTables)) {
73-
$this->raw($this->createTables);
86+
$this->createTables();
7487
}
7588
}
7689

7790
/**
78-
* SQL query, returns results in an array
91+
* @throws Exception
92+
*/
93+
private function createTables()
94+
{
95+
foreach ($this->createTables as $table) {
96+
$this->raw($table);
97+
}
98+
}
99+
100+
/**
101+
* SQL query, return results as an array
79102
*
80103
* @param string $sql
81104
* @param array $bind
82105
* @return array
83106
* @throws Exception
84107
*/
85-
public function query(string $sql, array $bind = []) :array
108+
public function query(string $sql, array $bind = []): array
86109
{
87110
if (!$this->connected) {
88111
$this->connect();
@@ -107,9 +130,10 @@ public function query(string $sql, array $bind = []) :array
107130
*
108131
* @param string $sql
109132
* @param array $bind
133+
* @return bool
110134
* @throws Exception
111135
*/
112-
public function raw(string $sql, array $bind = [])
136+
public function raw(string $sql, array $bind = []): bool
113137
{
114138
if (!$this->connected) {
115139
$this->connect();
@@ -121,7 +145,11 @@ public function raw(string $sql, array $bind = [])
121145
$result = $statement->execute($bind);
122146
if (!$result && ($this->pdo->errorCode() != '00000')) {
123147
$this->pdoFail('raw: execute statement failed');
148+
149+
return false;
124150
}
151+
152+
return true;
125153
}
126154

127155
/**

0 commit comments

Comments
 (0)