Skip to content

Commit 9251ef4

Browse files
committed
Starting to add sqlite support
1 parent 3828015 commit 9251ef4

File tree

5 files changed

+146
-1
lines changed

5 files changed

+146
-1
lines changed

src/Tqdev/PhpCrudApi/Config.php

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -41,6 +41,8 @@ private function getDefaultPort(string $driver): int
4141
return 5432;
4242
case 'sqlsrv':
4343
return 1433;
44+
case 'sqlite':
45+
return 0;
4446
}
4547
}
4648

@@ -53,6 +55,8 @@ private function getDefaultAddress(string $driver): string
5355
return 'localhost';
5456
case 'sqlsrv':
5557
return 'localhost';
58+
case 'sqlite':
59+
return 'data.db';
5660
}
5761
}
5862

src/Tqdev/PhpCrudApi/Database/GenericDB.php

Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -32,6 +32,8 @@ private function getDsn(): string
3232
return "$this->driver:host=$this->address port=$this->port dbname=$this->database options='--client_encoding=UTF8'";
3333
case 'sqlsrv':
3434
return "$this->driver:Server=$this->address,$this->port;Database=$this->database";
35+
case 'sqlite':
36+
return "$this->driver:$this->address";
3537
}
3638
}
3739

@@ -50,6 +52,10 @@ private function getCommands(): array
5052
];
5153
case 'sqlsrv':
5254
return [];
55+
case 'sqlite':
56+
return [
57+
'PRAGMA foreign_keys = on;',
58+
];
5359
}
5460
}
5561

@@ -76,6 +82,9 @@ private function getOptions(): array
7682
\PDO::SQLSRV_ATTR_DIRECT_QUERY => false,
7783
\PDO::SQLSRV_ATTR_FETCHES_NUMERIC_TYPE => true,
7884
];
85+
case 'sqlite':
86+
return $options + [
87+
];
7988
}
8089
}
8190

test.php

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -173,4 +173,4 @@ function run(array $drivers, string $dir, array $matches)
173173
}
174174
}
175175

176-
run(['mysql', 'pgsql', 'sqlsrv'], __DIR__ . '/tests', array_slice($argv, 1));
176+
run(['mysql', 'pgsql', 'sqlsrv', 'sqlite'], __DIR__ . '/tests', array_slice($argv, 1));

tests/config/sqlite.php

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,2 @@
1+
<?php
2+
$settings['driver'] = 'sqlite';

tests/fixtures/blog_sqlite.sql

Lines changed: 130 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -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+
--

0 commit comments

Comments
 (0)