Overview
Comment:DB: Ajoute la possibilité optionnelle de retourner un résultat sous forme d'objet plutôt que de tableau
Downloads: Tarball | ZIP archive | SQL archive
Timelines: family | ancestors | descendants | both | dev
Files: files | file ages | folders
SHA1: a2973bcf60cc525b1b688d25b025752f77c8c601
User & Date: bohwaz on 2017-03-09 04:29:55
Other Links: branch diff | manifest | tags
Context
2017-03-09
05:01
Ajout possibilité légende optionnelle pour fichiers (wiki), cf. [d7d50365514d2f84573773614877a8556dab5ee0] check-in: 8bf795cd47 user: bohwaz tags: dev
04:29
DB: Ajoute la possibilité optionnelle de retourner un résultat sous forme d'objet plutôt que de tableau check-in: a2973bcf60 user: bohwaz tags: dev
2017-03-07
04:32
Plus de tests unitaires basiques check-in: 764cdbfb00 user: bohwaz tags: dev
Changes

Modified src/include/lib/Garradin/DB.php from [b1c44ce023] to [51ccbaed54].

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25



26
27
28
29
30
31
32
<?php

namespace Garradin;

function str_replace_first ($search, $replace, $subject)
{
    $pos = strpos($subject, $search);

    if ($pos !== false)
    {
        $subject = substr_replace($subject, $replace, $pos, strlen($search));
    }

    return $subject;
}

class DB extends \SQLite3
{
    static protected $_instance = null;

    protected $_transaction = 0;

    const NUM = \SQLITE3_NUM;
    const ASSOC = \SQLITE3_ASSOC;
    const BOTH = \SQLITE3_BOTH;




    static public function getInstance($create = false)
    {
        return self::$_instance ?: self::$_instance = new DB($create);
    }

    private function __clone()




<
<
<
<
<
<
<
<
<
<
<
<









>
>
>







1
2
3
4












5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
<?php

namespace Garradin;













class DB extends \SQLite3
{
    static protected $_instance = null;

    protected $_transaction = 0;

    const NUM = \SQLITE3_NUM;
    const ASSOC = \SQLITE3_ASSOC;
    const BOTH = \SQLITE3_BOTH;
    const OBJ = 4; // SQLITE3_ASSOC, NUM and BOTH are 1, 2 and 3, so let's start at 4

    const ALL_COLUMNS = 1;

    static public function getInstance($create = false)
    {
        return self::$_instance ?: self::$_instance = new DB($create);
    }

    private function __clone()
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
        }
        catch (\Exception $e)
        {
            throw new \Exception($e->getMessage() . "\n" . $query . "\n" . json_encode($args, true));
        }
    }

    public function simpleStatementFetch($query, $mode = SQLITE3_BOTH)
    {
        if ($mode != SQLITE3_BOTH && $mode != SQLITE3_ASSOC && $mode != SQLITE3_NUM)
        {
            throw new \InvalidArgumentException('Mode argument should be either SQLITE3_BOTH, SQLITE3_ASSOC or SQLITE3_NUM.');
        }

        $args = array_slice(func_get_args(), 2);
        return $this->fetchResult($this->simpleStatement($query, $args), $mode);
    }

    public function simpleStatementFetchAssoc($query)
    {
        $args = array_slice(func_get_args(), 1);
        return $this->fetchResultAssoc($this->simpleStatement($query, $args));
    }

    public function simpleStatementFetchAssocKey($query, $mode = SQLITE3_BOTH)
    {
        if ($mode != SQLITE3_BOTH && $mode != SQLITE3_ASSOC && $mode != SQLITE3_NUM)
        {
            throw new \InvalidArgumentException('Mode argument should be either SQLITE3_BOTH, SQLITE3_ASSOC or SQLITE3_NUM.');
        }

        $args = array_slice(func_get_args(), 2);
        return $this->fetchResultAssocKey($this->simpleStatement($query, $args), $mode);
    }

    public function escapeAuto($value, $name = '')
    {
        $type = $this->getArgType($value, $name);







|

<
<
<
<
<










|

<
<
<
<
<







219
220
221
222
223
224
225
226
227





228
229
230
231
232
233
234
235
236
237
238
239





240
241
242
243
244
245
246
        }
        catch (\Exception $e)
        {
            throw new \Exception($e->getMessage() . "\n" . $query . "\n" . json_encode($args, true));
        }
    }

    public function simpleStatementFetch($query, $mode = null)
    {





        $args = array_slice(func_get_args(), 2);
        return $this->fetchResult($this->simpleStatement($query, $args), $mode);
    }

    public function simpleStatementFetchAssoc($query)
    {
        $args = array_slice(func_get_args(), 1);
        return $this->fetchResultAssoc($this->simpleStatement($query, $args));
    }

    public function simpleStatementFetchAssocKey($query, $mode = null)
    {





        $args = array_slice(func_get_args(), 2);
        return $this->fetchResultAssocKey($this->simpleStatement($query, $args), $mode);
    }

    public function escapeAuto($value, $name = '')
    {
        $type = $this->getArgType($value, $name);
308
309
310
311
312
313
314
315
316
317


318
319
320
321
322
323

324


325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349








350
351
352
353

354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379








380
381
382
383

384
385
386
387
388
389
390
391
392
393
394
395
396
397
398

399



400
401
402
403
404
405
     * Formats and escapes a statement and then returns the result of exec()
     */
    public function simpleExec($query)
    {
        return $this->simpleStatement($query, array_slice(func_get_args(), 1));
    }

    public function simpleQuerySingle($query, $all_columns = false)
    {
        $res = $this->simpleStatement($query, array_slice(func_get_args(), 2));



        $row = $res->fetchArray($all_columns ? SQLITE3_ASSOC : SQLITE3_NUM);

        if (!$all_columns)
        {
            if (isset($row[0]))

                return $row[0];


            return false;
        }
        else
        {
            return $row;
        }
    }

    public function queryFetch($query, $mode = SQLITE3_BOTH)
    {
        return $this->fetchResult($this->query($query), $mode);
    }

    public function queryFetchAssoc($query)
    {
        return $this->fetchResultAssoc($this->query($query));
    }

    public function queryFetchAssocKey($query, $mode = SQLITE3_BOTH)
    {
        return $this->fetchResultAssocKey($this->query($query), $mode);
    }

    public function fetchResult($result, $mode = \SQLITE3_BOTH)
    {








        $out = [];

        while ($row = $result->fetchArray($mode))
        {

            $out[] = $row;
        }

        $result->finalize();
        unset($result, $row);

        return $out;
    }

    protected function fetchResultAssoc($result)
    {
        $out = [];

        while ($row = $result->fetchArray(SQLITE3_NUM))
        {
            $out[$row[0]] = $row[1];
        }

        $result->finalize();
        unset($result, $row);

        return $out;
    }

    protected function fetchResultAssocKey($result, $mode = \SQLITE3_BOTH)
    {








        $out = [];

        while ($row = $result->fetchArray($mode))
        {

            $key = current($row);
            $out[$key] = $row;
        }

        $result->finalize();
        unset($result, $row);

        return $out;
    }

    public function countRows($result)
    {
        $i = 0;

        while ($result->fetchArray(SQLITE3_NUM))

            $i++;




        return $i;
    }
}

?>







|


>
>






>

>
>




|



|









|




|

>
>
>
>
>
>
>
>




>
|












|










|

>
>
>
>
>
>
>
>




>

|












|
>

>
>
>




<
<
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411


     * Formats and escapes a statement and then returns the result of exec()
     */
    public function simpleExec($query)
    {
        return $this->simpleStatement($query, array_slice(func_get_args(), 1));
    }

    public function simpleQuerySingle($query, $flags = false)
    {
        $res = $this->simpleStatement($query, array_slice(func_get_args(), 2));

        $all_columns = $flags & self::ALL_COLUMNS;

        $row = $res->fetchArray($all_columns ? SQLITE3_ASSOC : SQLITE3_NUM);

        if (!$all_columns)
        {
            if (isset($row[0]))
            {
                return $row[0];
            }

            return false;
        }
        else
        {
            return ($flags & self::OBJ) ? (object) $row : $row;
        }
    }

    public function queryFetch($query, $mode = null)
    {
        return $this->fetchResult($this->query($query), $mode);
    }

    public function queryFetchAssoc($query)
    {
        return $this->fetchResultAssoc($this->query($query));
    }

    public function queryFetchAssocKey($query, $mode = null)
    {
        return $this->fetchResultAssocKey($this->query($query), $mode);
    }

    public function fetchResult($result, $mode = null)
    {
        $as_obj = false;

        if ($mode === self::OBJ)
        {
            $as_obj = true;
            $mode = self::ASSOC;
        }

        $out = [];

        while ($row = $result->fetchArray($mode))
        {
            // FIXME: use generator (PHP 5.6+)
            $out[] = $as_obj ? (object) $row : $row;
        }

        $result->finalize();
        unset($result, $row);

        return $out;
    }

    protected function fetchResultAssoc($result)
    {
        $out = [];

        while ($row = $result->fetchArray(\SQLITE3_NUM))
        {
            $out[$row[0]] = $row[1];
        }

        $result->finalize();
        unset($result, $row);

        return $out;
    }

    protected function fetchResultAssocKey($result, $mode = null)
    {
        $as_obj = false;

        if ($mode === self::OBJ)
        {
            $as_obj = true;
            $mode = self::ASSOC;
        }

        $out = [];

        while ($row = $result->fetchArray($mode))
        {
            // FIXME: use generator (PHP 5.6+)
            $key = current($row);
            $out[$key] = $as_obj ? (object) $row : $row;
        }

        $result->finalize();
        unset($result, $row);

        return $out;
    }

    public function countRows($result)
    {
        $i = 0;

        while ($result->fetchArray(\SQLITE3_NUM))
        {
            $i++;
        }

        $result->reset();

        return $i;
    }
}