Browse Source

Got most of #405 implemented on master

Maurits van der Schee 5 years ago
parent
commit
6ca92f12db

+ 16
- 6
src/Tqdev/PhpCrudApi/Database/ColumnsBuilder.php View File

@@ -22,9 +22,14 @@ class ColumnsBuilder
22 22
             return '';
23 23
         }
24 24
         switch ($this->driver) {
25
-            case 'mysql':return " LIMIT $offset, $limit";
26
-            case 'pgsql':return " LIMIT $limit OFFSET $offset";
27
-            case 'sqlsrv':return " OFFSET $offset ROWS FETCH NEXT $limit ROWS ONLY";
25
+            case 'mysql':
26
+                return " LIMIT $offset, $limit";
27
+            case 'pgsql':
28
+                return " LIMIT $limit OFFSET $offset";
29
+            case 'sqlsrv':
30
+                return " OFFSET $offset ROWS FETCH NEXT $limit ROWS ONLY";
31
+            case 'sqlite':
32
+                return " LIMIT $limit OFFSET $offset";
28 33
         }
29 34
     }
30 35
 
@@ -74,9 +79,14 @@ class ColumnsBuilder
74 79
         $valuesSql = '(' . implode(',', $values) . ')';
75 80
         $outputColumn = $this->quoteColumnName($table->getPk());
76 81
         switch ($this->driver) {
77
-            case 'mysql':return "$columnsSql VALUES $valuesSql";
78
-            case 'pgsql':return "$columnsSql VALUES $valuesSql RETURNING $outputColumn";
79
-            case 'sqlsrv':return "$columnsSql OUTPUT INSERTED.$outputColumn VALUES $valuesSql";
82
+            case 'mysql':
83
+                return "$columnsSql VALUES $valuesSql";
84
+            case 'pgsql':
85
+                return "$columnsSql VALUES $valuesSql RETURNING $outputColumn";
86
+            case 'sqlsrv':
87
+                return "$columnsSql OUTPUT INSERTED.$outputColumn VALUES $valuesSql";
88
+            case 'sqlite':
89
+                return "$columnsSql VALUES $valuesSql";
80 90
         }
81 91
     }
82 92
 

+ 2
- 2
src/Tqdev/PhpCrudApi/Database/DataConverter.php View File

@@ -27,10 +27,10 @@ class DataConverter
27 27
 
28 28
     private function getRecordValueConversion(ReflectedColumn $column): string
29 29
     {
30
-        if (in_array($this->driver, ['mysql', 'sqlsrv']) && $column->isBoolean()) {
30
+        if (in_array($this->driver, ['mysql', 'sqlsrv', 'sqlite']) && $column->isBoolean()) {
31 31
             return 'boolean';
32 32
         }
33
-        if ($this->driver == 'sqlsrv' && $column->getType() == 'bigint') {
33
+        if (in_array($this->driver, ['sqlsrv', 'sqlite']) && in_array($column->getType(), ['integer', 'bigint'])) {
34 34
             return 'integer';
35 35
         }
36 36
         return 'none';

+ 7
- 2
src/Tqdev/PhpCrudApi/Database/GenericDB.php View File

@@ -83,8 +83,7 @@ class GenericDB
83 83
                     \PDO::SQLSRV_ATTR_FETCHES_NUMERIC_TYPE => true,
84 84
                 ];
85 85
             case 'sqlite':
86
-                return $options + [
87
-                ];
86
+                return $options + [];
88 87
         }
89 88
     }
90 89
 
@@ -192,11 +191,17 @@ class GenericDB
192 191
             case 'mysql':
193 192
                 $stmt = $this->query('SELECT LAST_INSERT_ID()', []);
194 193
                 break;
194
+            case 'sqlite':
195
+                $stmt = $this->query('SELECT LAST_INSERT_ROWID()', []);
196
+                break;
195 197
         }
196 198
         $pkValue = $stmt->fetchColumn(0);
197 199
         if ($this->driver == 'sqlsrv' && $table->getPk()->getType() == 'bigint') {
198 200
             return (int) $pkValue;
199 201
         }
202
+        if ($this->driver == 'sqlite' && in_array($table->getPk()->getType(), ['integer', 'bigint'])) {
203
+            return (int) $pkValue;
204
+        }
200 205
         return $pkValue;
201 206
     }
202 207
 

+ 71
- 51
src/Tqdev/PhpCrudApi/Database/GenericReflection.php View File

@@ -23,50 +23,47 @@ class GenericReflection
23 23
 
24 24
     private function createSqlLiteReflectionTables() /*: void */
25 25
     {
26
-        $reflection = $this->query('SELECT "name" FROM "sqlite_master" WHERE "type" = \'table\' and name like \'sys/%\';',[]);
27
-        if (count($reflection->fetchAll())==0) {
28
-			//create reflection tables
29
-			$this->query('CREATE table "sys/version" ("version" integer);',[]);
30
-			$this->query('CREATE table "sys/tables" ("name" text, "type" text);',[]);
31
-			$this->query('CREATE table "sys/columns" ("self" text,"cid" integer,"name" text,"type" integer,"notnull" integer,"dflt_value" integer,"pk" integer);',[]);
32
-			$this->query('CREATE table "sys/foreign_keys" ("self" text,"id" integer,"seq" integer,"table" text,"from" text,"to" text,"on_update" text,"on_delete" text,"match" text);',[]);
33
-		}
34
-        $version = $this->query('pragma schema_version',[])->fetchColumn(0);
35
-		if ($version != $this->query('SELECT "version" from "sys/version";')->fetchColumn(0)) {
36
-			// reflection may take a while
37
-			set_time_limit(3600);
38
-			// update version data
39
-			$this->query('DELETE FROM "sys/version";');
40
-            $stmt = $this->pdo->prepare('INSERT into "sys/version" ("version") VALUES (?);');
41
-            $stmt->execute(array($version)); 
42
-
43
-			// update tables data
44
-			$this->query('DELETE FROM "sys/tables";');
45
-			$result = $this->query('SELECT "name", "type" FROM sqlite_master WHERE ("type" = \'table\' or "type" = \'view\') and name not like "sys/%" and name<>"sqlite_sequence";');
46
-			$tables = array();
47
-			foreach($result as $row) {
48
-				$tables[] = $row['name'];
49
-                $stmt = $this->pdo->prepare('INSERT into "sys/tables" ("name", "type") VALUES (?, ?);');
50
-                $stmt->execute(array($row['name'], $row['type']));
51
-			}
52
-			// update columns and foreign_keys data
53
-			$this->query('DELETE FROM "sys/columns"');
54
-			$this->query('DELETE FROM "sys/foreign_keys"');
55
-			foreach ($tables as $table) {
56
-				$result = $this->query("pragma table_info(`$table`);");
57
-				foreach($result as $row) {
58
-					array_unshift($row, $table);
59
-                    $stmt = $this->pdo->prepare('INSERT into "sys/columns" ("self","cid","name","type","notnull","dflt_value","pk") VALUES (?,?,?,?,?,?,?);');
60
-                    $stmt->execute(array_values($row));
61
-				}
62
-				$result = $this->query("pragma foreign_key_list(`$table`);");
63
-				foreach($result as $row) {
64
-					array_unshift($row, $table);
65
-                    $stmt = $this->pdo->prepare('INSERT into "sys/foreign_keys" ("self","id","seq","table","from","to","on_update","on_delete","match") VALUES (?,?,?,?,?,?,?,?,?);');
66
-                    $stmt->execute(array_values($row));
67
-				}
68
-			}
69
-		}
26
+        $reflection = $this->query('SELECT "name" FROM "sqlite_master" WHERE "type" = \'table\' and name like \'sys/%\';', []);
27
+        if (count($reflection) == 0) {
28
+            //create reflection tables
29
+            $this->query('CREATE table "sys/version" ("version" integer);', []);
30
+            $this->query('CREATE table "sys/tables" ("name" text, "type" text);', []);
31
+            $this->query('CREATE table "sys/columns" ("self" text,"cid" integer,"name" text,"type" integer,"notnull" integer,"dflt_value" integer,"pk" integer);', []);
32
+            $this->query('CREATE table "sys/foreign_keys" ("self" text,"id" integer,"seq" integer,"table" text,"from" text,"to" text,"on_update" text,"on_delete" text,"match" text);', []);
33
+        }
34
+        $version = $this->query('pragma schema_version;', [])[0]["schema_version"];
35
+        $current = $this->query('SELECT "version" from "sys/version";', []);
36
+        if (!$current || count($current) == 0 || !isset($current[0]["schema_version"]) || $version != $current[0]["schema_version"]) {
37
+            // reflection may take a while
38
+            set_time_limit(3600);
39
+            // update version data
40
+            $this->query('DELETE FROM "sys/version";', []);
41
+            $this->query('INSERT into "sys/version" ("version") VALUES (?);', [$version]);
42
+
43
+            // update tables data
44
+            $this->query('DELETE FROM "sys/tables";', []);
45
+            $result = $this->query('SELECT "name", "type" FROM sqlite_master WHERE ("type" = \'table\' or "type" = \'view\') and name not like "sys/%" and name<>"sqlite_sequence";', []);
46
+            $tables = array();
47
+            foreach ($result as $row) {
48
+                $tables[] = $row['name'];
49
+                $this->query('INSERT into "sys/tables" ("name", "type") VALUES (?, ?);', [$row['name'], $row['type']]);
50
+            }
51
+            // update columns and foreign_keys data
52
+            $this->query('DELETE FROM "sys/columns";', []);
53
+            $this->query('DELETE FROM "sys/foreign_keys";', []);
54
+            foreach ($tables as $table) {
55
+                $result = $this->query("pragma table_info(`$table`);", []);
56
+                foreach ($result as $row) {
57
+                    array_unshift($row, $table);
58
+                    $this->query('INSERT into "sys/columns" ("self","cid","name","type","notnull","dflt_value","pk") VALUES (?,?,?,?,?,?,?);', array_values($row));
59
+                }
60
+                $result = $this->query("pragma foreign_key_list(`$table`);", []);
61
+                foreach ($result as $row) {
62
+                    array_unshift($row, $table);
63
+                    $this->query('INSERT into "sys/foreign_keys" ("self","id","seq","table","from","to","on_update","on_delete","match") VALUES (?,?,?,?,?,?,?,?,?);', array_values($row));
64
+                }
65
+            }
66
+        }
70 67
     }
71 68
 
72 69
     public function getIgnoredTables(): array
@@ -79,12 +76,10 @@ class GenericReflection
79 76
             case 'sqlsrv':
80 77
                 return [];
81 78
             case 'sqlite':
82
-                return ['sys/version','sys/tables','sys/columns','sys/foreign_keys'];
79
+                return ['sys/version', 'sys/tables', 'sys/columns', 'sys/foreign_keys'];
83 80
         }
84 81
     }
85 82
 
86
-
87
-
88 83
     private function getTablesSQL(): string
89 84
     {
90 85
         switch ($this->driver) {
@@ -96,7 +91,7 @@ class GenericReflection
96 91
                 return 'SELECT o.name as "TABLE_NAME", o.xtype as "TABLE_TYPE" FROM sysobjects o WHERE o.xtype IN (\'U\', \'V\') ORDER BY "TABLE_NAME"';
97 92
             case 'sqlite':
98 93
                 $this->createSqlLiteReflectionTables();
99
-                return 'SELECT t.name as "TABLE_NAME", t.type as "TABLE_TYPE" FROM "sys/tables" t WHERE t.type IN (\'table\', \'view\') ORDER BY "TABLE_NAME"';
94
+                return 'SELECT t.name as "TABLE_NAME", t.type as "TABLE_TYPE" FROM "sys/tables" t WHERE t.type IN (\'table\', \'view\') AND \'\' <> ? ORDER BY "TABLE_NAME"';
100 95
         }
101 96
     }
102 97
 
@@ -109,6 +104,8 @@ class GenericReflection
109 104
                 return 'SELECT a.attname AS "COLUMN_NAME", case when a.attnotnull then \'NO\' else \'YES\' end as "IS_NULLABLE", pg_catalog.format_type(a.atttypid, -1) as "DATA_TYPE", case when a.atttypmod < 0 then NULL else a.atttypmod-4 end as "CHARACTER_MAXIMUM_LENGTH", case when a.atttypid != 1700 then NULL else ((a.atttypmod - 4) >> 16) & 65535 end as "NUMERIC_PRECISION", case when a.atttypid != 1700 then NULL else (a.atttypmod - 4) & 65535 end as "NUMERIC_SCALE", \'\' AS "COLUMN_TYPE" FROM pg_attribute a JOIN pg_class pgc ON pgc.oid = a.attrelid WHERE pgc.relname = ? AND \'\' <> ? AND a.attnum > 0 AND NOT a.attisdropped;';
110 105
             case 'sqlsrv':
111 106
                 return 'SELECT c.name AS "COLUMN_NAME", c.is_nullable AS "IS_NULLABLE", t.Name AS "DATA_TYPE", (c.max_length/2) AS "CHARACTER_MAXIMUM_LENGTH", c.precision AS "NUMERIC_PRECISION", c.scale AS "NUMERIC_SCALE", \'\' AS "COLUMN_TYPE" FROM sys.columns c INNER JOIN sys.types t ON c.user_type_id = t.user_type_id WHERE c.object_id = OBJECT_ID(?) AND \'\' <> ?';
107
+            case 'sqlite':
108
+                return 'SELECT "name" AS "COLUMN_NAME", case when "notnull"==1 then \'no\' else \'yes\' end as "IS_NULLABLE", "type" AS "DATA_TYPE", 2147483647 AS "CHARACTER_MAXIMUM_LENGTH", 0 AS "NUMERIC_PRECISION", 0 AS "NUMERIC_SCALE", \'\' AS "COLUMN_TYPE" FROM "sys/columns" WHERE "self" = ? AND \'\' <> ?';
112 109
         }
113 110
     }
114 111
 
@@ -121,6 +118,8 @@ class GenericReflection
121 118
                 return 'SELECT a.attname AS "COLUMN_NAME" FROM pg_attribute a JOIN pg_constraint c ON (c.conrelid, c.conkey[1]) = (a.attrelid, a.attnum) JOIN pg_class pgc ON pgc.oid = a.attrelid WHERE pgc.relname = ? AND \'\' <> ? AND c.contype = \'p\'';
122 119
             case 'sqlsrv':
123 120
                 return 'SELECT c.NAME as "COLUMN_NAME" FROM sys.key_constraints kc inner join sys.objects t on t.object_id = kc.parent_object_id INNER JOIN sys.index_columns ic ON kc.parent_object_id = ic.object_id and kc.unique_index_id = ic.index_id INNER JOIN sys.columns c ON ic.object_id = c.object_id AND ic.column_id = c.column_id WHERE kc.type = \'PK\' and t.object_id = OBJECT_ID(?) and \'\' <> ?';
121
+            case 'sqlite':
122
+                return 'SELECT "name" as "COLUMN_NAME" FROM "sys/columns" WHERE "pk"=1 AND "self"=? AND \'\' <> ?';
124 123
         }
125 124
     }
126 125
 
@@ -133,6 +132,8 @@ class GenericReflection
133 132
                 return 'SELECT a.attname AS "COLUMN_NAME", c.confrelid::regclass::text AS "REFERENCED_TABLE_NAME" FROM pg_attribute a JOIN pg_constraint c ON (c.conrelid, c.conkey[1]) = (a.attrelid, a.attnum) JOIN pg_class pgc ON pgc.oid = a.attrelid WHERE pgc.relname = ? AND \'\' <> ? AND c.contype  = \'f\'';
134 133
             case 'sqlsrv':
135 134
                 return 'SELECT COL_NAME(fc.parent_object_id, fc.parent_column_id) AS "COLUMN_NAME", OBJECT_NAME (f.referenced_object_id) AS "REFERENCED_TABLE_NAME" FROM sys.foreign_keys AS f INNER JOIN sys.foreign_key_columns AS fc ON f.OBJECT_ID = fc.constraint_object_id WHERE f.parent_object_id = OBJECT_ID(?) and \'\' <> ?';
135
+            case 'sqlite':
136
+                return 'SELECT "from" AS "COLUMN_NAME", "table" AS "REFERENCED_TABLE_NAME" FROM "sys/foreign_keys" WHERE "self" = ? AND \'\' <> ?';
136 137
         }
137 138
     }
138 139
 
@@ -150,20 +151,22 @@ class GenericReflection
150 151
             return !$tables || in_array($v['TABLE_NAME'], $tables);
151 152
         });
152 153
         foreach ($results as &$result) {
154
+            $map = [];
153 155
             switch ($this->driver) {
154 156
                 case 'mysql':
155 157
                     $map = ['BASE TABLE' => 'table', 'VIEW' => 'view'];
156
-                    $result['TABLE_TYPE'] = $map[$result['TABLE_TYPE']];
157 158
                     break;
158 159
                 case 'pgsql':
159 160
                     $map = ['r' => 'table', 'v' => 'view'];
160
-                    $result['TABLE_TYPE'] = $map[$result['TABLE_TYPE']];
161 161
                     break;
162 162
                 case 'sqlsrv':
163 163
                     $map = ['U' => 'table', 'V' => 'view'];
164
-                    $result['TABLE_TYPE'] = $map[trim($result['TABLE_TYPE'])];
164
+                    break;
165
+                case 'sqlite':
166
+                    $map = ['table' => 'table', 'view' => 'view'];
165 167
                     break;
166 168
             }
169
+            $result['TABLE_TYPE'] = $map[trim($result['TABLE_TYPE'])];
167 170
         }
168 171
         return $results;
169 172
     }
@@ -192,6 +195,23 @@ class GenericReflection
192 195
                 }
193 196
             }
194 197
         }
198
+        if ($this->driver == 'sqlite') {
199
+            foreach ($results as &$result) {
200
+                // mysql does not properly reflect display width of types
201
+                preg_match('|([a-z]+)(\(([0-9]+)(,([0-9]+))?\))?|', $result['DATA_TYPE'], $matches);
202
+                if (isset($matches[1])) {
203
+                    $result['DATA_TYPE'] = $matches[1];
204
+                } else {
205
+                    $result['DATA_TYPE'] = 'text';
206
+                }
207
+                if (isset($matches[5])) {
208
+                    $result['NUMERIC_PRECISION'] = $matches[3];
209
+                    $result['NUMERIC_SCALE'] = $matches[5];
210
+                } else if (isset($matches[3])) {
211
+                    $result['CHARACTER_MAXIMUM_LENGTH'] = $matches[3];
212
+                }
213
+            }
214
+        }
195 215
         return $results;
196 216
     }
197 217
 

+ 14
- 0
src/Tqdev/PhpCrudApi/Database/TypeConverter.php View File

@@ -121,6 +121,20 @@ class TypeConverter
121 121
             'uniqueidentifier' => 'char',
122 122
             'xml' => 'clob',
123 123
         ],
124
+        'sqlite' => [
125
+            'tinytext' => 'clob',
126
+            'text' => 'clob',
127
+            'mediumtext' => 'clob',
128
+            'longtext' => 'clob',
129
+            'mediumint' => 'integer',
130
+            'int' => 'integer',
131
+            'bigint' => 'bigint',
132
+            'int2' => 'smallint',
133
+            'int4' => 'integer',
134
+            'int8' => 'bigint',
135
+            'double precision' => 'double',
136
+            'datetime' => 'timestamp',
137
+        ],
124 138
     ];
125 139
 
126 140
     // source: https://docs.oracle.com/javase/9/docs/api/java/sql/Types.html

+ 2
- 0
src/Tqdev/PhpCrudApi/Middleware/Router/SimpleRouter.php View File

@@ -141,6 +141,8 @@ class SimpleRouter implements Router
141 141
         } catch (\PDOException $e) {
142 142
             if (strpos(strtolower($e->getMessage()), 'duplicate') !== false) {
143 143
                 $response = $this->responder->error(ErrorCode::DUPLICATE_KEY_EXCEPTION, '');
144
+            } elseif (strpos(strtolower($e->getMessage()), 'unique constraint') !== false) {
145
+                $response = $this->responder->error(ErrorCode::DUPLICATE_KEY_EXCEPTION, '');
144 146
             } elseif (strpos(strtolower($e->getMessage()), 'default value') !== false) {
145 147
                 $response = $this->responder->error(ErrorCode::DATA_INTEGRITY_VIOLATION, '');
146 148
             } elseif (strpos(strtolower($e->getMessage()), 'allow nulls') !== false) {

+ 62
- 36
tests/fixtures/blog_sqlite.sql View File

@@ -5,27 +5,31 @@ PRAGMA foreign_keys = off;
5 5
 DROP TABLE IF EXISTS "categories";
6 6
 CREATE TABLE "categories" (
7 7
   "id" integer NOT NULL PRIMARY KEY AUTOINCREMENT,
8
-  "name" text(255) NOT NULL,
9
-  "icon" data NULL
8
+  "name" varchar(255) NOT NULL,
9
+  "icon" blob NULL
10 10
 );
11 11
 
12
-INSERT INTO "categories" ("id", "name", "icon") VALUES (1,	'announcement',	NULL);
13
-INSERT INTO "categories" ("id", "name", "icon") VALUES (2,	'article',	NULL);
12
+INSERT INTO "categories" ("id", "name", "icon") VALUES (1, 'announcement', NULL);
13
+INSERT INTO "categories" ("id", "name", "icon") VALUES (2, 'article', NULL);
14
+INSERT INTO "categories" ("id", "name", "icon") VALUES (3, 'comment', NULL);
14 15
 
15 16
 DROP TABLE IF EXISTS "comments";
16 17
 CREATE TABLE "comments" (
17 18
   "id" integer NOT NULL PRIMARY KEY AUTOINCREMENT,
18 19
   "post_id" integer NOT NULL,
19 20
   "message" text NOT NULL,
20
-  FOREIGN KEY ("post_id") REFERENCES "posts" ("id")
21
+  "category_id" integer NOT NULL,
22
+  FOREIGN KEY ("post_id") REFERENCES "posts" ("id"),
23
+  FOREIGN KEY ("category_id") REFERENCES "categories" ("id")
21 24
 );
22 25
 
23 26
 CREATE INDEX "comments_post_id" ON "comments" ("post_id");
27
+CREATE INDEX "comments_category_id" ON "comments" ("category_id");
24 28
 
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
+INSERT INTO "comments" ("id", "post_id", "message", "category_id") VALUES (1, 1, 'great', 3);
30
+INSERT INTO "comments" ("id", "post_id", "message", "category_id") VALUES (2, 1, 'fantastic', 3);
31
+INSERT INTO "comments" ("id", "post_id", "message", "category_id") VALUES (3, 2, 'thank you', 3);
32
+INSERT INTO "comments" ("id", "post_id", "message", "category_id") VALUES (4, 2, 'awesome', 3);
29 33
 
30 34
 DROP TABLE IF EXISTS "post_tags";
31 35
 CREATE TABLE "post_tags" (
@@ -38,10 +42,10 @@ CREATE TABLE "post_tags" (
38 42
 
39 43
 CREATE UNIQUE INDEX "post_tags_post_id_tag_id" ON "post_tags" ("post_id", "tag_id");
40 44
 
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
+INSERT INTO "post_tags" ("id", "post_id", "tag_id") VALUES (1, 1, 1);
46
+INSERT INTO "post_tags" ("id", "post_id", "tag_id") VALUES (2, 1, 2);
47
+INSERT INTO "post_tags" ("id", "post_id", "tag_id") VALUES (3, 2, 1);
48
+INSERT INTO "post_tags" ("id", "post_id", "tag_id") VALUES (4, 2, 2);
45 49
 
46 50
 DROP TABLE IF EXISTS "posts";
47 51
 CREATE TABLE "posts" (
@@ -57,48 +61,58 @@ CREATE INDEX "posts_user_id" ON "posts" ("user_id");
57 61
 
58 62
 CREATE INDEX "posts_category_id" ON "posts" ("category_id");
59 63
 
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!');
64
+INSERT INTO "posts" ("id", "user_id", "category_id", "content") VALUES (1, 1, 1, 'blog started');
65
+INSERT INTO "posts" ("id", "user_id", "category_id", "content") VALUES (2, 1, 2, 'It works!');
62 66
 
63 67
 DROP TABLE IF EXISTS "tags";
64 68
 CREATE TABLE "tags" (
65 69
   "id" integer NOT NULL PRIMARY KEY AUTOINCREMENT,
66
-  "name" text(255) NOT NULL
70
+  "name" varchar(255) NOT NULL,
71
+  "is_important" boolean NOT NULL
67 72
 );
68 73
 
69
-INSERT INTO "tags" ("id", "name") VALUES (1,	'funny');
70
-INSERT INTO "tags" ("id", "name") VALUES (2,	'important');
74
+INSERT INTO "tags" ("id", "name", "is_important") VALUES (1, 'funny', 0);
75
+INSERT INTO "tags" ("id", "name", "is_important") VALUES (2, 'important', 1);
71 76
 
72 77
 DROP TABLE IF EXISTS "users";
73 78
 CREATE TABLE "users" (
74 79
   "id" integer NOT NULL PRIMARY KEY AUTOINCREMENT,
75
-  "username" text(255) NOT NULL,
76
-  "password" text(255) NOT NULL,
77
-  "location" geometry NULL
80
+  "username" varchar(255) NOT NULL,
81
+  "password" varchar(255) NOT NULL,
82
+  "location" clob NULL
78 83
 );
79 84
 
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);
85
+INSERT INTO "users" ("id", "username", "password", "location") VALUES (1, 'user1', 'pass1', NULL);
86
+INSERT INTO "users" ("id", "username", "password", "location") VALUES (2, 'user2', 'pass2', NULL);
82 87
 
83 88
 DROP TABLE IF EXISTS "countries";
84 89
 CREATE TABLE "countries" (
85 90
   "id" integer NOT NULL PRIMARY KEY AUTOINCREMENT,
86
-  "name" text(255) NOT NULL,
87
-  "shape" geometry NOT NULL
91
+  "name" varchar(255) NOT NULL,
92
+  "shape" clob NOT NULL
88 93
 );
89 94
 
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))');
95
+INSERT INTO "countries" ("id", "name", "shape") VALUES (1, 'Left', 'POLYGON ((30 10, 40 40, 20 40, 10 20, 30 10))');
96
+INSERT INTO "countries" ("id", "name", "shape") VALUES (2, 'Right', 'POLYGON ((70 10, 80 40, 60 40, 50 20, 70 10))');
97
+INSERT INTO "countries" ("id", "name", "shape") VALUES (3, 'Point', 'POINT (30 10)');
98
+INSERT INTO "countries" ("id", "name", "shape") VALUES (4, 'Line', 'LINESTRING (30 10, 10 30, 40 40)');
99
+INSERT INTO "countries" ("id", "name", "shape") VALUES (5, 'Poly1', 'POLYGON ((30 10, 40 40, 20 40, 10 20, 30 10))');
100
+INSERT INTO "countries" ("id", "name", "shape") VALUES (6, 'Poly2', 'POLYGON ((35 10, 45 45, 15 40, 10 20, 35 10),(20 30, 35 35, 30 20, 20 30))');
101
+INSERT INTO "countries" ("id", "name", "shape") VALUES (7, 'Mpoint', 'MULTIPOINT (10 40, 40 30, 20 20, 30 10)');
102
+INSERT INTO "countries" ("id", "name", "shape") VALUES (8, 'Mline', 'MULTILINESTRING ((10 10, 20 20, 10 40),(40 40, 30 30, 40 20, 30 10))');
103
+INSERT INTO "countries" ("id", "name", "shape") VALUES (9, 'Mpoly1', 'MULTIPOLYGON (((30 20, 45 40, 10 40, 30 20)),((15 5, 40 10, 10 20, 5 10, 15 5)))');
104
+INSERT INTO "countries" ("id", "name", "shape") VALUES (10, 'Mpoly2', 'MULTIPOLYGON (((40 40, 20 45, 45 30, 40 40)),((20 35, 10 30, 10 10, 30 5, 45 20, 20 35),(30 20, 20 15, 20 25, 30 20)))');
105
+INSERT INTO "countries" ("id", "name", "shape") VALUES (11, 'Gcoll', 'GEOMETRYCOLLECTION(POINT(4 6),LINESTRING(4 6,7 10))');
92 106
 
93 107
 DROP TABLE IF EXISTS "events";
94 108
 CREATE TABLE "events" (
95 109
   "id" integer NOT NULL PRIMARY KEY AUTOINCREMENT,
96
-  "name" text(255) NOT NULL,
110
+  "name" varchar(255) NOT NULL,
97 111
   "datetime" datetime NOT NULL,
98 112
   "visitors" integer NOT NULL
99 113
 );
100 114
 
101
-INSERT INTO "events" ("id", "name", "datetime", "visitors") VALUES (1,	'Launch',	'2016-01-01 13:01:01',	0);
115
+INSERT INTO "events" ("id", "name", "datetime", "visitors") VALUES (1, 'Launch', '2016-01-01 13:01:01', 0);
102 116
 
103 117
 DROP VIEW IF EXISTS "tag_usage";
104 118
 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";
@@ -106,24 +120,36 @@ CREATE VIEW "tag_usage" AS select "name", count("name") AS "count" from "tags",
106 120
 DROP TABLE IF EXISTS "products";
107 121
 CREATE TABLE "products" (
108 122
   "id" integer NOT NULL PRIMARY KEY AUTOINCREMENT,
109
-  "name" text(255) NOT NULL,
110
-  "price" text(12) NOT NULL,
111
-  "properties" json NOT NULL,
123
+  "name" varchar(255) NOT NULL,
124
+  "price" varchar(12) NOT NULL,
125
+  "properties" clob NOT NULL,
112 126
   "created_at" datetime NOT NULL,
113 127
   "deleted_at" datetime NULL
114 128
 );
115 129
 
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');
130
+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 131
 
118 132
 DROP TABLE IF EXISTS "barcodes";
119 133
 CREATE TABLE "barcodes" (
120 134
   "id" integer NOT NULL PRIMARY KEY AUTOINCREMENT,
121 135
   "product_id" integer NOT NULL,
122
-  "hex" text(255) NOT NULL,
123
-  "bin" binary(255) NOT NULL
136
+  "hex" varchar(255) NOT NULL,
137
+  "bin" blob NOT NULL
138
+);
139
+
140
+INSERT INTO "barcodes" ("id", "product_id", "hex", "bin") VALUES (1, 1, '00ff01', 'AP8B');
141
+
142
+DROP TABLE IF EXISTS "kunsthåndværk";
143
+CREATE TABLE "kunsthåndværk" (
144
+  "id" varchar(36) NOT NULL PRIMARY KEY,
145
+  "Umlauts ä_ö_ü-COUNT" integer NOT NULL UNIQUE,
146
+  "user_id" integer NOT NULL,
147
+  "invisible" varchar(36),
148
+  FOREIGN KEY ("user_id") REFERENCES "users" ("id") ON DELETE RESTRICT ON UPDATE RESTRICT
124 149
 );
125 150
 
126
-INSERT INTO "barcodes" ("id", "product_id", "hex", "bin") VALUES (1,	1,	'00ff01',	'AP8B');
151
+INSERT INTO "kunsthåndværk" ("id", "Umlauts ä_ö_ü-COUNT", "user_id", "invisible") VALUES ('e42c77c6-06a4-4502-816c-d112c7142e6d', 1, 1, NULL);
152
+INSERT INTO "kunsthåndværk" ("id", "Umlauts ä_ö_ü-COUNT", "user_id", "invisible") VALUES ('e31ecfe6-591f-4660-9fbd-1a232083037f', 2, 2, NULL);
127 153
 
128 154
 PRAGMA foreign_keys = on;
129 155
 

Loading…
Cancel
Save