PHPackages                             matatirosoln/sql-to-odata - PHPackages - PHPackages  [Skip to content](#main-content)[PHPackages](/)[Directory](/)[Categories](/categories)[Trending](/trending)[Leaderboard](/leaderboard)[Changelog](/changelog)[Analyze](/analyze)[Collections](/collections)[Log in](/login)[Sign up](/register)

1. [Directory](/)
2. /
3. [API Development](/categories/api)
4. /
5. matatirosoln/sql-to-odata

ActiveLibrary[API Development](/categories/api)

matatirosoln/sql-to-odata
=========================

Convert SQL queries to OData query syntax.

0.0.1(5d ago)02↑2900%1MITPHPPHP ^8.4

Since Jun 4Pushed todayCompare

[ Source](https://github.com/matatirosolutions/sql-to-odata)[ Packagist](https://packagist.org/packages/matatirosoln/sql-to-odata)[ RSS](/packages/matatirosoln-sql-to-odata/feed)WikiDiscussions main Synced today

READMEChangelogDependencies (2)Versions (2)Used By (1)

MSDev SQL to OData
==================

[](#msdev-sql-to-odata)

A PHP library for converting SQL queries to OData (Open Data Protocol) query syntax. Targets **OData 4.01** at the intermediate conformance level, primarily designed for use with the Claris FileMaker OData API (FileMaker Server 2025 / v22+).

We would be very interested in hearing from potential users who have access to alternative OData implementations and may be interested in working to extend this library to ensure that it is broadly useful as possible.

Requirements
------------

[](#requirements)

- PHP ^8.4
- Composer

Installation
------------

[](#installation)

```
composer require matatirosoln/sql-to-odata
```

Usage
-----

[](#usage)

Call `parse()` on any supported SQL statement. It returns a typed query object you can inspect to build your OData request.

```
use Matatirosoln\SqlToOdata\SqlToOdata;

$converter = new SqlToOdata();
$query = $converter->parse($sql);
```

### SELECT

[](#select)

Returns a `SelectQuery` with `entitySet` and `queryString` properties.

```
use Matatirosoln\SqlToOdata\Query\SelectQuery;

$query = $converter->parse("SELECT Id, Name FROM Users WHERE Status = 'Active' ORDER BY Name ASC LIMIT 10");
// $query->entitySet   => 'Users'
// $query->queryString => '?$select=Id,Name&$filter=Status eq \'Active\'&$orderby=Name asc&$top=10'

assert($query instanceof SelectQuery);
```

### INSERT

[](#insert)

Returns an `InsertQuery` with `entitySet` and `body` properties. `body` is an associative array of column-value pairs with PHP-native types, suitable for JSON-encoding into a POST request body.

```
use Matatirosoln\SqlToOdata\Query\InsertQuery;

$query = $converter->parse("INSERT INTO Users (Name, Age, Active) VALUES ('John', 30, true)");
// $query->entitySet => 'Users'
// $query->body      => ['Name' => 'John', 'Age' => 30, 'Active' => true]

assert($query instanceof InsertQuery);
```

### UPDATE

[](#update)

Returns an `UpdateQuery` with `entitySet`, `body`, and `filter` properties. `filter` is an OData filter expression derived from the WHERE clause. UPDATE without a WHERE clause throws a `ConversionException`.

```
use Matatirosoln\SqlToOdata\Query\UpdateQuery;

$query = $converter->parse("UPDATE Users SET Name = 'Jane', Age = 31 WHERE Id = 1");
// $query->entitySet => 'Users'
// $query->body      => ['Name' => 'Jane', 'Age' => 31]
// $query->filter    => 'Id eq 1'

assert($query instanceof UpdateQuery);
```

### DELETE

[](#delete)

Returns a `DeleteQuery` with `entitySet` and `filter` properties. DELETE without a WHERE clause throws a `ConversionException`.

```
use Matatirosoln\SqlToOdata\Query\DeleteQuery;

$query = $converter->parse('DELETE FROM Users WHERE Id = 1');
// $query->entitySet => 'Users'
// $query->filter    => 'Id eq 1'

assert($query instanceof DeleteQuery);
```

### Dispatching on query type

[](#dispatching-on-query-type)

Use `match` or `instanceof` checks to handle each statement type:

```
use Matatirosoln\SqlToOdata\Query\DeleteQuery;
use Matatirosoln\SqlToOdata\Query\InsertQuery;
use Matatirosoln\SqlToOdata\Query\SelectQuery;
use Matatirosoln\SqlToOdata\Query\UpdateQuery;

$query = $converter->parse($sql);

match (true) {
    $query instanceof SelectQuery => handleSelect($query),
    $query instanceof InsertQuery => handleInsert($query),
    $query instanceof UpdateQuery => handleUpdate($query),
    $query instanceof DeleteQuery => handleDelete($query),
};
```

Supported SQL features
----------------------

[](#supported-sql-features)

> Targets OData 4.01 intermediate conformance level ([spec](http://docs.oasis-open.org/odata/odata/v4.01/os/part1-protocol/odata-v4.01-os-part1-protocol.html)).

### SELECT clauses

[](#select-clauses)

SQLOData`SELECT col1, col2``$select=col1,col2``SELECT *`*(omitted — returns all fields)*`WHERE``$filter``ORDER BY``$orderby``LIMIT n``$top=n``LIMIT n OFFSET m``$top=n&$skip=m`### WHERE operators

[](#where-operators)

SQLOData`=``eq``!=` / ```ne``>``gt``>=``ge``
