@@ -5,6 +5,10 @@ class FreshRSS_CategoryDAO extends Minz_ModelPdo {
5
5
6
6
public const DEFAULTCATEGORYID = 1 ;
7
7
8
+ public function sqlResetSequence (): bool {
9
+ return true ; // Nothing to do for MySQL
10
+ }
11
+
8
12
public function resetDefaultCategoryName (): bool {
9
13
//FreshRSS 1.15.1
10
14
$ stm = $ this ->pdo ->prepare ('UPDATE `_category` SET name = :name WHERE id = :id ' );
@@ -101,32 +105,49 @@ protected function autoUpdateDb(array $errorInfo): bool {
101
105
}
102
106
103
107
/**
104
- * @param array{name:string, id?:int,kind?:int,lastUpdate?:int,error?:int|bool,attributes?:string|array<string,mixed>} $valuesTmp
108
+ * @param array{id?:int,name:string ,kind?:int,lastUpdate?:int,error?:int|bool,attributes?:string|array<string,mixed>} $valuesTmp
105
109
*/
106
110
public function addCategory (array $ valuesTmp ): int |false {
107
- // TRIM() to provide a type hint as text
111
+ if (empty ($ valuesTmp ['id ' ])) { // Auto-generated ID
112
+ $ sql = <<<'SQL'
113
+ INSERT INTO `_category`(name, kind, attributes)
114
+ SELECT * FROM (SELECT :name1 AS name, 1*:kind AS kind, :attributes AS attributes) c2
115
+ SQL;
116
+ } else {
117
+ $ sql = <<<'SQL'
118
+ INSERT INTO `_category`(id, name, kind, attributes)
119
+ SELECT * FROM (SELECT 1*:id AS id, :name1 AS name, 1*:kind AS kind, :attributes AS attributes) c2
120
+ SQL;
121
+ }
108
122
// No tag of the same name
109
- $ sql = <<<'SQL'
110
- INSERT INTO `_category`(kind, name, attributes)
111
- SELECT * FROM (SELECT ABS(?) AS kind, TRIM(?) AS name, TRIM(?) AS attributes) c2
112
- WHERE NOT EXISTS (SELECT 1 FROM `_tag` WHERE name = TRIM(?))
123
+ $ sql .= "\n" . <<<'SQL'
124
+ WHERE NOT EXISTS (SELECT 1 FROM `_tag` WHERE name = :name2)
113
125
SQL;
114
126
$ stm = $ this ->pdo ->prepare ($ sql );
115
127
116
128
$ valuesTmp ['name ' ] = mb_strcut (trim ($ valuesTmp ['name ' ]), 0 , FreshRSS_DatabaseDAO::LENGTH_INDEX_UNICODE , 'UTF-8 ' );
117
129
if (!isset ($ valuesTmp ['attributes ' ])) {
118
130
$ valuesTmp ['attributes ' ] = [];
119
131
}
120
- $ values = [
121
- $ valuesTmp ['kind ' ] ?? FreshRSS_Category::KIND_NORMAL ,
122
- $ valuesTmp ['name ' ],
123
- is_string ($ valuesTmp ['attributes ' ]) ? $ valuesTmp ['attributes ' ] : json_encode ($ valuesTmp ['attributes ' ], JSON_UNESCAPED_SLASHES | JSON_UNESCAPED_UNICODE ),
124
- $ valuesTmp ['name ' ],
125
- ];
126
-
127
- if ($ stm !== false && $ stm ->execute ($ values ) && $ stm ->rowCount () > 0 ) {
128
- $ catId = $ this ->pdo ->lastInsertId ('`_category_id_seq` ' );
129
- return $ catId === false ? false : (int )$ catId ;
132
+ if ($ stm !== false ) {
133
+ if (!empty ($ valuesTmp ['id ' ])) {
134
+ $ stm ->bindValue (':id ' , $ valuesTmp ['id ' ], PDO ::PARAM_INT );
135
+ }
136
+ $ stm ->bindValue (':name1 ' , $ valuesTmp ['name ' ], PDO ::PARAM_STR );
137
+ $ stm ->bindValue (':kind ' , $ valuesTmp ['kind ' ] ?? FreshRSS_Category::KIND_NORMAL , PDO ::PARAM_INT );
138
+ $ attributes = is_string ($ valuesTmp ['attributes ' ]) ? $ valuesTmp ['attributes ' ] :
139
+ json_encode ($ valuesTmp ['attributes ' ], JSON_UNESCAPED_SLASHES | JSON_UNESCAPED_UNICODE );
140
+ $ stm ->bindValue (':attributes ' , $ attributes , PDO ::PARAM_STR );
141
+ $ stm ->bindValue (':name2 ' , $ valuesTmp ['name ' ], PDO ::PARAM_STR );
142
+ }
143
+ if ($ stm !== false && $ stm ->execute () && $ stm ->rowCount () > 0 ) {
144
+ if (empty ($ valuesTmp ['id ' ])) {
145
+ // Auto-generated ID
146
+ $ catId = $ this ->pdo ->lastInsertId ('`_category_id_seq` ' );
147
+ return $ catId === false ? false : (int )$ catId ;
148
+ }
149
+ $ this ->sqlResetSequence ();
150
+ return $ valuesTmp ['id ' ];
130
151
} else {
131
152
$ info = $ stm === false ? $ this ->pdo ->errorInfo () : $ stm ->errorInfo ();
132
153
/** @var array{0:string,1:int,2:string} $info */
@@ -207,9 +228,6 @@ public function updateLastUpdate(int $id, bool $inError = false, int $mtime = 0)
207
228
}
208
229
209
230
public function deleteCategory (int $ id ): int |false {
210
- if ($ id <= self ::DEFAULTCATEGORYID ) {
211
- return false ;
212
- }
213
231
$ sql = 'DELETE FROM `_category` WHERE id=:id ' ;
214
232
$ stm = $ this ->pdo ->prepare ($ sql );
215
233
if ($ stm !== false && $ stm ->bindParam (':id ' , $ id , PDO ::PARAM_INT ) && $ stm ->execute ()) {
@@ -355,10 +373,6 @@ public function checkDefault(): int|bool {
355
373
$ cat = new FreshRSS_Category (_t ('gen.short.default_category ' ), self ::DEFAULTCATEGORYID );
356
374
357
375
$ sql = 'INSERT INTO `_category`(id, name) VALUES(?, ?) ' ;
358
- if ($ this ->pdo ->dbType () === 'pgsql ' ) {
359
- //Force call to nextval()
360
- $ sql .= " RETURNING nextval('`_category_id_seq`'); " ;
361
- }
362
376
$ stm = $ this ->pdo ->prepare ($ sql );
363
377
364
378
$ values = [
@@ -368,6 +382,7 @@ public function checkDefault(): int|bool {
368
382
369
383
if ($ stm !== false && $ stm ->execute ($ values )) {
370
384
$ catId = $ this ->pdo ->lastInsertId ('`_category_id_seq` ' );
385
+ $ this ->sqlResetSequence ();
371
386
return $ catId === false ? false : (int )$ catId ;
372
387
} else {
373
388
$ info = $ stm === false ? $ this ->pdo ->errorInfo () : $ stm ->errorInfo ();
0 commit comments