summaryrefslogtreecommitdiff
path: root/src/FirebirdConnection.php
blob: 92640aff3b4b7044875c2e911b5022feea642d34 (plain)
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
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
<?php

namespace Xgrz\Firebird;

use Illuminate\Database\Connection as DatabaseConnection;
use Illuminate\Support\Collection;
use Xgrz\Firebird\Query\Builder as FirebirdQueryBuilder;
use Xgrz\Firebird\Query\Grammars\FirebirdGrammar as FirebirdQueryGrammar;
use Xgrz\Firebird\Query\Processors\FirebirdProcessor as FirebirdQueryProcessor;
use Xgrz\Firebird\Schema\Builder as FirebirdSchemaBuilder;
use Xgrz\Firebird\Schema\Grammars\FirebirdGrammar as FirebirdSchemaGrammar;

class FirebirdConnection extends DatabaseConnection
{
    /**
     * The last inserted ID generated by the server.
	 * RETURNED by the insert statement
     *
     * @var string|int|null
     */
    protected $lastInsertId;

    /**
     * Get the default query grammar instance.
     *
     * @return \Illuminate\Database\Query\Grammars\Grammar
     */
    protected function getDefaultQueryGrammar(): FirebirdQueryGrammar
    {
        return new FirebirdQueryGrammar($this);
    }

    /**
     * Get the default post processor instance.
     *
     * @return \Illuminate\Database\Query\Processors\Processor
     */
    protected function getDefaultPostProcessor(): FirebirdQueryProcessor
    {
        return new FirebirdQueryProcessor;
    }

    /**
     * Get a schema builder instance for this connection.
     *
     * @return \Xgrz\Firebird\Schema\Builder
     */
    public function getSchemaBuilder(): FirebirdSchemaBuilder
    {
        if (is_null($this->schemaGrammar)) {
            $this->useDefaultSchemaGrammar();
        }

        return new FirebirdSchemaBuilder($this);
    }

    /**
     * Get the default schema grammar instance.
     *
     * @return \Xgrz\Firebird\Schema\Grammars\FirebirdGrammar
     */
    protected function getDefaultSchemaGrammar()
    {
        return new FirebirdSchemaGrammar($this); // $this->withTablePrefix()
    }

    /**
     * Get a new query builder instance.
     *
     * @return \Xgrz\Firebird\Query\Builder
     */
    public function query()
    {
        return new FirebirdQueryBuilder(
            $this, $this->getQueryGrammar(), $this->getPostProcessor()
        );
    }

    /**
     * Run an insert statement against the database.
     *
     * @param  string  $query
     * @param  array  $bindings
     * @param  string|null  $sequence
     * @return bool
     */
    public function insert($query, $bindings = [], $sequence = null)
    {
        return $this->run($query, $bindings, function ($query, $bindings) use ($sequence) {
            if ($this->pretending()) {
                return true;
            }
			
			$statement = $this->getPdo()->prepare($query);

            $this->bindValues($statement, $this->prepareBindings($bindings));

            $this->recordsHaveBeenModified();

            $result = $statement->execute();

			// Fetch the RETURNING clause result to get the actual ID used
			if ($result) {
				$row = $statement->fetch(\PDO::FETCH_ASSOC);
				if ($row) {
					// Get the first (and typically only) returned value
					$this->lastInsertId = reset($row);
				}
			}
			
           return $result;
        });
    }
    /**
     * Execute a stored procedure.
     *
     * @param  string  $procedure
     * @param  array  $values
     * @return Collection
     */
    public function executeProcedure($procedure, array $values = []): Collection
    {
        return $this->query()->fromProcedure($procedure, $values)->get();
    }
}