Skip to content

Commit

Permalink
Use constructor property promotion and remove deprecated methods
Browse files Browse the repository at this point in the history
  • Loading branch information
theodorejb committed Oct 13, 2024
1 parent c04b0b2 commit d582e33
Show file tree
Hide file tree
Showing 6 changed files with 21 additions and 217 deletions.
48 changes: 0 additions & 48 deletions lib/BaseOptions.php
Original file line number Diff line number Diff line change
Expand Up @@ -38,52 +38,4 @@ public function escapeIdentifier(string $identifier): string
$qualifiedIdentifiers = array_map($escaper, explode('.', $identifier));
return implode('.', $qualifiedIdentifiers);
}

/**
* Specify the maximum number of parameters which can be bound in a single query.
* If greater than zero, PeachySQL will batch insert queries to avoid the limit.
* @deprecated Use public property instead
* @api
*/
public function setMaxBoundParams(int $maxParams): void
{
if ($maxParams < 0) {
throw new \InvalidArgumentException('The maximum number of bound parameters must be greater than or equal to zero');
}

$this->maxBoundParams = $maxParams;
}

/**
* @deprecated Use public property instead
* @api
*/
public function getMaxBoundParams(): int
{
return $this->maxBoundParams;
}

/**
* Specify the maximum number of rows which can be inserted via a single query.
* If greater than zero, PeachySQL will batch insert queries to remove the limit.
* @deprecated Use public property instead
* @api
*/
public function setMaxInsertRows(int $maxRows): void
{
if ($maxRows < 0) {
throw new \InvalidArgumentException('The maximum number of insert rows must be greater than or equal to zero');
}

$this->maxInsertRows = $maxRows;
}

/**
* @deprecated Use public property instead
* @api
*/
public function getMaxInsertRows(): int
{
return $this->maxInsertRows;
}
}
63 changes: 8 additions & 55 deletions lib/BulkInsertResult.php
Original file line number Diff line number Diff line change
Expand Up @@ -6,64 +6,17 @@

/**
* Object returned when performing bulk insert queries
* @readonly
*/
class BulkInsertResult
{
/**
* The IDs of the inserted rows
* @var list<int>
* @param list<int> $ids The IDs of the inserted rows
* @param int $affected The number of affected rows
* @param int $queryCount The number of individual queries used to perform the bulk insert
*/
public array $ids;

/**
* The number of affected rows
*/
public int $affected;

/**
* The number of individual queries used to perform the bulk insert
*/
public int $queryCount;

/**
* @param list<int> $ids
*/
public function __construct(array $ids, int $affected, int $queryCount = 1)
{
$this->ids = $ids;
$this->affected = $affected;
$this->queryCount = $queryCount;
}

/**
* Returns the IDs of the inserted rows
* @deprecated Use readonly property instead
* @return list<int>
* @api
*/
public function getIds(): array
{
return $this->ids;
}

/**
* Returns the number of affected rows
* @deprecated Use readonly property instead
* @api
*/
public function getAffected(): int
{
return $this->affected;
}

/**
* Returns the number of individual queries used to perform the bulk insert
* @deprecated Use readonly property instead
* @api
*/
public function getQueryCount(): int
{
return $this->queryCount;
}
public function __construct(
public readonly array $ids,
public readonly int $affected,
public readonly int $queryCount = 1
) {}
}
45 changes: 6 additions & 39 deletions lib/InsertResult.php
Original file line number Diff line number Diff line change
Expand Up @@ -6,48 +6,15 @@

/**
* Object returned when inserting a single row
* @readonly
*/
class InsertResult
{
/**
* The ID of the inserted row (0 if the row doesn't have an auto-incremented ID)
* @param int $id The ID of the inserted row (0 if the row doesn't have an auto-incremented ID)
* @param int $affected The number of affected rows
*/
public int $id;

/**
* The number of affected rows
*/
public int $affected;

public function __construct(int $id, int $affected)
{
$this->id = $id;
$this->affected = $affected;
}

/**
* Returns the ID of the inserted row
* @throws \Exception if the row doesn't have an auto-incremented ID
* @deprecated Use readonly property instead
* @api
*/
public function getId(): int
{
if ($this->id === 0) {
throw new \Exception('Inserted row does not have an auto-incremented ID');
}

return $this->id;
}

/**
* Returns the number of affected rows
* @deprecated Use readonly property instead
* @api
*/
public function getAffected(): int
{
return $this->affected;
}
public function __construct(
public readonly int $id,
public readonly int $affected
) {}
}
10 changes: 0 additions & 10 deletions lib/PeachySql.php
Original file line number Diff line number Diff line change
Expand Up @@ -48,16 +48,6 @@ abstract public function query(string $sql, array $params = []): BaseStatement;
*/
abstract protected function insertBatch(string $table, array $colVals, int $identityIncrement = 1): BulkInsertResult;

/**
* Returns the current PeachySQL options
* @deprecated Use public readonly property instead
* @api
*/
public function getOptions(): BaseOptions
{
return $this->options;
}

public function selectFrom(string $query): QueryableSelector
{
return new QueryableSelector(new SqlParams($query, []), $this);
Expand Down
33 changes: 4 additions & 29 deletions lib/QueryBuilder/SqlParams.php
Original file line number Diff line number Diff line change
Expand Up @@ -6,39 +6,14 @@

/**
* Represents a SQL query and its corresponding parameters
* @readonly
*/
class SqlParams
{
public string $sql;
/** @var list<mixed> */
public array $params;

/**
* @param list<mixed> $params
*/
public function __construct(string $sql, array $params)
{
$this->sql = $sql;
$this->params = $params;
}

/**
* @deprecated Use readonly property instead
* @api
*/
public function getSql(): string
{
return $this->sql;
}

/**
* @return list<mixed>
* @deprecated Use readonly property instead
* @api
*/
public function getParams(): array
{
return $this->params;
}
public function __construct(
public readonly string $sql,
public readonly array $params,
) {}
}
39 changes: 3 additions & 36 deletions lib/SqlException.php
Original file line number Diff line number Diff line change
Expand Up @@ -11,21 +11,18 @@ class SqlException extends \RuntimeException
{
/**
* The error list returned by the database driver
* @readonly
*/
public array $errors;
public readonly array $errors;

/**
* The failed SQL query
* @readonly
*/
public string $query;
public readonly string $query;

/**
* The failed query's bound parameters
* @readonly
*/
public array $params;
public readonly array $params;

/**
* @param string $msg The error message
Expand Down Expand Up @@ -63,34 +60,4 @@ public function getSqlState(): string
// MySQL SQL Server
return $this->errors[0]['sqlstate'] ?? $this->errors[0]['SQLSTATE'] ?? '';
}

/**
* Returns the list of errors from sqlsrv_errors() or mysqli::$error_list
* @deprecated Use readonly property instead
* @api
*/
public function getErrors(): array
{
return $this->errors;
}

/**
* Returns the failed SQL query
* @deprecated Use readonly property instead
* @api
*/
public function getQuery(): string
{
return $this->query;
}

/**
* Returns the array of bound parameters
* @deprecated Use readonly property instead
* @api
*/
public function getParams(): array
{
return $this->params;
}
}

0 comments on commit d582e33

Please sign in to comment.