Browse Source

Starting to add sqlite support

Maurits van der Schee 5 years ago
parent
commit
9251ef4712

+ 4
- 0
src/Tqdev/PhpCrudApi/Config.php View File

@@ -41,6 +41,8 @@ class Config
41 41
                 return 5432;
42 42
             case 'sqlsrv':
43 43
                 return 1433;
44
+            case 'sqlite':
45
+                return 0;
44 46
         }
45 47
     }
46 48
 
@@ -53,6 +55,8 @@ class Config
53 55
                 return 'localhost';
54 56
             case 'sqlsrv':
55 57
                 return 'localhost';
58
+            case 'sqlite':
59
+                return 'data.db';
56 60
         }
57 61
     }
58 62
 

+ 9
- 0
src/Tqdev/PhpCrudApi/Database/GenericDB.php View File

@@ -32,6 +32,8 @@ class GenericDB
32 32
                 return "$this->driver:host=$this->address port=$this->port dbname=$this->database options='--client_encoding=UTF8'";
33 33
             case 'sqlsrv':
34 34
                 return "$this->driver:Server=$this->address,$this->port;Database=$this->database";
35
+            case 'sqlite':
36
+                return "$this->driver:$this->address";
35 37
         }
36 38
     }
37 39
 
@@ -50,6 +52,10 @@ class GenericDB
50 52
                 ];
51 53
             case 'sqlsrv':
52 54
                 return [];
55
+            case 'sqlite':
56
+                return [
57
+                    'PRAGMA foreign_keys = on;',
58
+                ];
53 59
         }
54 60
     }
55 61
 
@@ -76,6 +82,9 @@ class GenericDB
76 82
                     \PDO::SQLSRV_ATTR_DIRECT_QUERY => false,
77 83
                     \PDO::SQLSRV_ATTR_FETCHES_NUMERIC_TYPE => true,
78 84
                 ];
85
+            case 'sqlite':
86
+                return $options + [
87
+                ];
79 88
         }
80 89
     }
81 90
 

+ 1
- 1
test.php View File

@@ -173,4 +173,4 @@ function run(array $drivers, string $dir, array $matches)
173 173
     }
174 174
 }
175 175
 
176
-run(['mysql', 'pgsql', 'sqlsrv'], __DIR__ . '/tests', array_slice($argv, 1));
176
+run(['mysql', 'pgsql', 'sqlsrv', 'sqlite'], __DIR__ . '/tests', array_slice($argv, 1));

+ 2
- 0
tests/config/sqlite.php View File

@@ -0,0 +1,2 @@
1
+<?php
2
+$settings['driver'] = 'sqlite';

+ 130
- 0
tests/fixtures/blog_sqlite.sql View File

@@ -0,0 +1,130 @@
1
+-- Adminer 4.2.4 SQLite 3 dump
2
+
3
+PRAGMA foreign_keys = off;
4
+
5
+DROP TABLE IF EXISTS "categories";
6
+CREATE TABLE "categories" (
7
+  "id" integer NOT NULL PRIMARY KEY AUTOINCREMENT,
8
+  "name" text(255) NOT NULL,
9
+  "icon" data NULL
10
+);
11
+
12
+INSERT INTO "categories" ("id", "name", "icon") VALUES (1,	'announcement',	NULL);
13
+INSERT INTO "categories" ("id", "name", "icon") VALUES (2,	'article',	NULL);
14
+
15
+DROP TABLE IF EXISTS "comments";
16
+CREATE TABLE "comments" (
17
+  "id" integer NOT NULL PRIMARY KEY AUTOINCREMENT,
18
+  "post_id" integer NOT NULL,
19
+  "message" text NOT NULL,
20
+  FOREIGN KEY ("post_id") REFERENCES "posts" ("id")
21
+);
22
+
23
+CREATE INDEX "comments_post_id" ON "comments" ("post_id");
24
+
25
+INSERT INTO "comments" ("id", "post_id", "message") VALUES (1,	1,	'great');
26
+INSERT INTO "comments" ("id", "post_id", "message") VALUES (2,	1,	'fantastic');
27
+INSERT INTO "comments" ("id", "post_id", "message") VALUES (3,	2,	'thank you');
28
+INSERT INTO "comments" ("id", "post_id", "message") VALUES (4,	2,	'awesome');
29
+
30
+DROP TABLE IF EXISTS "post_tags";
31
+CREATE TABLE "post_tags" (
32
+  "id" integer NOT NULL PRIMARY KEY AUTOINCREMENT,
33
+  "post_id" integer NOT NULL,
34
+  "tag_id" integer NOT NULL,
35
+  FOREIGN KEY ("tag_id") REFERENCES "tags" ("id"),
36
+  FOREIGN KEY ("post_id") REFERENCES "posts" ("id") ON DELETE RESTRICT ON UPDATE RESTRICT
37
+);
38
+
39
+CREATE UNIQUE INDEX "post_tags_post_id_tag_id" ON "post_tags" ("post_id", "tag_id");
40
+
41
+INSERT INTO "post_tags" ("id", "post_id", "tag_id") VALUES (1,	1,	1);
42
+INSERT INTO "post_tags" ("id", "post_id", "tag_id") VALUES (2,	1,	2);
43
+INSERT INTO "post_tags" ("id", "post_id", "tag_id") VALUES (3,	2,	1);
44
+INSERT INTO "post_tags" ("id", "post_id", "tag_id") VALUES (4,	2,	2);
45
+
46
+DROP TABLE IF EXISTS "posts";
47
+CREATE TABLE "posts" (
48
+  "id" integer NOT NULL PRIMARY KEY AUTOINCREMENT,
49
+  "user_id" integer NOT NULL,
50
+  "category_id" integer NOT NULL,
51
+  "content" text NOT NULL,
52
+  FOREIGN KEY ("user_id") REFERENCES "users" ("id") ON DELETE RESTRICT ON UPDATE RESTRICT,
53
+  FOREIGN KEY ("category_id") REFERENCES "categories" ("id") ON DELETE RESTRICT ON UPDATE RESTRICT
54
+);
55
+
56
+CREATE INDEX "posts_user_id" ON "posts" ("user_id");
57
+
58
+CREATE INDEX "posts_category_id" ON "posts" ("category_id");
59
+
60
+INSERT INTO "posts" ("id", "user_id", "category_id", "content") VALUES (1,	1,	1,	'blog started');
61
+INSERT INTO "posts" ("id", "user_id", "category_id", "content") VALUES (2,	1,	2,	'It works!');
62
+
63
+DROP TABLE IF EXISTS "tags";
64
+CREATE TABLE "tags" (
65
+  "id" integer NOT NULL PRIMARY KEY AUTOINCREMENT,
66
+  "name" text(255) NOT NULL
67
+);
68
+
69
+INSERT INTO "tags" ("id", "name") VALUES (1,	'funny');
70
+INSERT INTO "tags" ("id", "name") VALUES (2,	'important');
71
+
72
+DROP TABLE IF EXISTS "users";
73
+CREATE TABLE "users" (
74
+  "id" integer NOT NULL PRIMARY KEY AUTOINCREMENT,
75
+  "username" text(255) NOT NULL,
76
+  "password" text(255) NOT NULL,
77
+  "location" geometry NULL
78
+);
79
+
80
+INSERT INTO "users" ("id", "username", "password", "location") VALUES (1,	'user1',	'pass1',	NULL);
81
+INSERT INTO "users" ("id", "username", "password", "location") VALUES (2,	'user2',	'pass2',	NULL);
82
+
83
+DROP TABLE IF EXISTS "countries";
84
+CREATE TABLE "countries" (
85
+  "id" integer NOT NULL PRIMARY KEY AUTOINCREMENT,
86
+  "name" text(255) NOT NULL,
87
+  "shape" geometry NOT NULL
88
+);
89
+
90
+INSERT INTO "countries" ("id", "name", "shape") VALUES (1,	'Left',	'POLYGON ((30 10, 40 40, 20 40, 10 20, 30 10))');
91
+INSERT INTO "countries" ("id", "name", "shape") VALUES (2,	'Right',	'POLYGON ((70 10, 80 40, 60 40, 50 20, 70 10))');
92
+
93
+DROP TABLE IF EXISTS "events";
94
+CREATE TABLE "events" (
95
+  "id" integer NOT NULL PRIMARY KEY AUTOINCREMENT,
96
+  "name" text(255) NOT NULL,
97
+  "datetime" datetime NOT NULL,
98
+  "visitors" integer NOT NULL
99
+);
100
+
101
+INSERT INTO "events" ("id", "name", "datetime", "visitors") VALUES (1,	'Launch',	'2016-01-01 13:01:01',	0);
102
+
103
+DROP VIEW IF EXISTS "tag_usage";
104
+CREATE VIEW "tag_usage" AS select "name", count("name") AS "count" from "tags", "post_tags" where "tags"."id" = "post_tags"."tag_id" group by "name" order by "count" desc, "name";
105
+
106
+DROP TABLE IF EXISTS "products";
107
+CREATE TABLE "products" (
108
+  "id" integer NOT NULL PRIMARY KEY AUTOINCREMENT,
109
+  "name" text(255) NOT NULL,
110
+  "price" text(12) NOT NULL,
111
+  "properties" json NOT NULL,
112
+  "created_at" datetime NOT NULL,
113
+  "deleted_at" datetime NULL
114
+);
115
+
116
+INSERT INTO "products" ("id", "name", "price", "properties", "created_at") VALUES (1,	'Calculator',	'23.01',	'{"depth":false,"model":"TRX-120","width":100,"height":null}',	'1970-01-01 01:01:01');
117
+
118
+DROP TABLE IF EXISTS "barcodes";
119
+CREATE TABLE "barcodes" (
120
+  "id" integer NOT NULL PRIMARY KEY AUTOINCREMENT,
121
+  "product_id" integer NOT NULL,
122
+  "hex" text(255) NOT NULL,
123
+  "bin" binary(255) NOT NULL
124
+);
125
+
126
+INSERT INTO "barcodes" ("id", "product_id", "hex", "bin") VALUES (1,	1,	'00ff01',	'AP8B');
127
+
128
+PRAGMA foreign_keys = on;
129
+
130
+--

Loading…
Cancel
Save