├── README.md ├── composer.json └── src ├── ODBCBuilder.php ├── ODBCConnection.php ├── ODBCConnector.php ├── ODBCPdo.php ├── ODBCPdoStatement.php ├── ODBCProcessor.php └── ODBCServiceProvider.php /README.md: -------------------------------------------------------------------------------- 1 | ## ODBC integration for Laravel Framework 2 | This integration allows the use of odbc_* php function with Laravel framework instead of PDO.
3 | It emulates PDO class used by Laravel. 4 | 5 | ### # How to install 6 | > `composer require abram/laravel-odbc` To add source in your project 7 | 8 | ### # Usage Instructions 9 | It's very simple to configure: 10 | 11 | **1) Add database to database.php file** 12 | ```PHP 13 | 'odbc-connection-name' => [ 14 | 'driver' => 'odbc', 15 | 'dsn' => 'OdbcConnectionName', 16 | 'database' => 'DatabaseName', 17 | 'host' => '127.0.0.1', 18 | 'username' => 'username', 19 | 'password' => 'password' 20 | ] 21 | ``` 22 | 23 | **2) Add service provider in app.php file** 24 | ```PHP 25 | 'providers' => [ 26 | ... 27 | Abram\Odbc\ODBCServiceProvider::class 28 | ] 29 | ``` 30 | 31 | ### # Eloquen ORM 32 | You can use Laravel, Eloquent ORM and other Illuminate's components as usual. 33 | ```PHP 34 | # Facade 35 | $books = DB::connection('odbc-connection-name')->table('books')->where('Author', 'Abram Andrea')->get(); 36 | 37 | # ORM 38 | $books = Book::where('Author', 'Abram Andrea')->get(); 39 | ``` 40 | 41 | ### # Custom getLastInsertId() function 42 | If you want to provide a custom getLastInsertId() function, you can extends *ODBCProcessor* class and override function.
43 | ```PHP 44 | class CustomProcessor extends ODBCProcessor 45 | { 46 | /** 47 | * @param Builder $query 48 | * @param null $sequence 49 | * @return mixed 50 | */ 51 | public function getLastInsertId(Builder $query, $sequence = null) 52 | { 53 | return $query->getConnection()->table($query->from)->latest('id')->first()->getAttribute($sequence); 54 | } 55 | } 56 | ``` 57 | 58 | ### # Custom Processor / QueryGrammar / SchemaGrammar 59 | To use another class instead default one you can update your connection in: 60 | ```PHP 61 | 'odbc-connection-name' => [ 62 | 'driver' => 'odbc', 63 | 'dsn' => 'OdbcConnectionName', 64 | 'database' => 'DatabaseName', 65 | 'host' => '127.0.0.1', 66 | 'username' => 'username', 67 | 'password' => 'password', 68 | 'options' => [ 69 | 'processor' => Illuminate\Database\Query\Processors\Processor::class, //default 70 | 'grammar' => [ 71 | 'query' => Illuminate\Database\Query\Grammars\Grammar::class, //default 72 | 'schema' => Illuminate\Database\Schema\Grammars\Grammar::class //default 73 | ] 74 | ] 75 | ] 76 | ``` 77 | -------------------------------------------------------------------------------- /composer.json: -------------------------------------------------------------------------------- 1 | { 2 | "name": "abram/laravel-odbc", 3 | "description": "ODBC integration for Laravel framework ", 4 | "type": "library", 5 | "homepage": "https://github.com/andreossido/laravel-odbc", 6 | "authors": [ 7 | { 8 | "name": "Andrea Abram", 9 | "email": "andreossido@gmail.com", 10 | "homepage": "https://github.com/andreossido", 11 | "role": "Developer" 12 | } 13 | ], 14 | "require": { 15 | "php": "^5.3.3 || >=7.0", 16 | "illuminate/database": "~5.1 || >= 6.0", 17 | "illuminate/support": "~5.1 || >= 6.0" 18 | }, 19 | "autoload": { 20 | "classmap": [ 21 | "src" 22 | ] 23 | }, 24 | "extra": { 25 | "laravel": { 26 | "providers": [ 27 | "Abram\\Odbc\\ODBCServiceProvider" 28 | ] 29 | } 30 | } 31 | } 32 | -------------------------------------------------------------------------------- /src/ODBCBuilder.php: -------------------------------------------------------------------------------- 1 | model = $model; 27 | return parent::__construct($connection, $grammar, $processor); 28 | } 29 | 30 | public function whereIn($column, $values, $boolean = 'and', $not = false) 31 | { 32 | return parent::whereIn($column, $values, $boolean, $not); 33 | } 34 | 35 | 36 | public function where($column, $operator = null, $value = null, $boolean = 'and') 37 | { 38 | list($value, $operator) = $this->prepareValueAndOperator( 39 | $value, $operator, func_num_args() == 2 40 | ); 41 | $value = $this->getModel()->wrapAttribute($column, $value); 42 | return parent::where($column, $operator, $value, $boolean); 43 | } 44 | 45 | /** 46 | * @return BaseModel 47 | */ 48 | public function getModel(): BaseModel 49 | { 50 | return $this->model; 51 | } 52 | 53 | /** 54 | * @param BaseModel $model 55 | */ 56 | public function setModel(BaseModel $model): void 57 | { 58 | $this->model = $model; 59 | } 60 | } -------------------------------------------------------------------------------- /src/ODBCConnection.php: -------------------------------------------------------------------------------- 1 | getConfig('options.grammar.query'); 18 | if ($queryGrammar) 19 | return new $queryGrammar; 20 | return parent::getDefaultQueryGrammar(); 21 | } 22 | 23 | function getDefaultSchemaGrammar() 24 | { 25 | $schemaGrammar = $this->getConfig('options.grammar.schema'); 26 | if ($schemaGrammar) 27 | return new $schemaGrammar; 28 | return parent::getDefaultSchemaGrammar(); 29 | } 30 | 31 | /** 32 | * Get the default post processor instance. 33 | * 34 | * @return ODBCProcessor 35 | */ 36 | 37 | 38 | protected function getDefaultPostProcessor() 39 | { 40 | $processor = $this->getConfig('options.processor'); 41 | if ($processor) 42 | return new $processor; 43 | return new ODBCProcessor; 44 | } 45 | } -------------------------------------------------------------------------------- /src/ODBCConnector.php: -------------------------------------------------------------------------------- 1 | getOptions($config); 30 | 31 | $dsn = Arr::get($config, 'dsn'); 32 | 33 | $connection = $this->createConnection($dsn, $config, $options); 34 | 35 | return $connection; 36 | } 37 | 38 | /** 39 | * Create a new PDO connection instance. 40 | * 41 | * @param string $dsn 42 | * @param string $username 43 | * @param string $password 44 | * @param array $options 45 | * @return ODBCPdo 46 | */ 47 | protected function createPdoConnection($dsn, $username, $password, $options) 48 | { 49 | return new ODBCPdo($dsn, $username, $password); 50 | } 51 | } 52 | -------------------------------------------------------------------------------- /src/ODBCPdo.php: -------------------------------------------------------------------------------- 1 | setConnection(odbc_connect($dsn, $username, $passwd)); 21 | } 22 | 23 | public function exec($query) 24 | { 25 | return $this->prepare($query)->execute(); 26 | } 27 | 28 | public function prepare($statement, $driver_options = null) 29 | { 30 | return new ODBCPdoStatement($this->getConnection(), $statement); 31 | } 32 | 33 | /** 34 | * @return mixed 35 | */ 36 | public function getConnection() 37 | { 38 | return $this->connection; 39 | } 40 | 41 | /** 42 | * @param mixed $connection 43 | */ 44 | public function setConnection($connection): void 45 | { 46 | $this->connection = $connection; 47 | } 48 | 49 | /** 50 | * @param null $name 51 | * @return string|void 52 | * @throws Exception 53 | */ 54 | public function lastInsertId($name = null) 55 | { 56 | throw new Exception("Error, you must override this method!"); 57 | } 58 | 59 | public function commit() 60 | { 61 | return odbc_commit($this->getConnection()); 62 | } 63 | 64 | public function rollBack() 65 | { 66 | $rollback = odbc_rollback($this->getConnection()); 67 | odbc_autocommit($this->getConnection(), true); 68 | return $rollback; 69 | } 70 | 71 | public function beginTransaction() 72 | { 73 | odbc_autocommit($this->getConnection(), false); 74 | } 75 | } -------------------------------------------------------------------------------- /src/ODBCPdoStatement.php: -------------------------------------------------------------------------------- 1 | query = preg_replace('/(?<=\s|^):[^\s:]++/um', '?', $query); 22 | 23 | $this->params = $this->getParamsFromQuery($query); 24 | 25 | $this->statement = odbc_prepare($conn, $this->query); 26 | } 27 | 28 | protected function getParamsFromQuery($qry) 29 | { 30 | $params = []; 31 | $qryArray = explode(" ", $qry); 32 | $i = 0; 33 | 34 | while (isset($qryArray[$i])) { 35 | if (preg_match("/^:/", $qryArray[$i])) 36 | $params[$qryArray[$i]] = null; 37 | $i++; 38 | } 39 | 40 | return $params; 41 | } 42 | 43 | public function rowCount() 44 | { 45 | return odbc_num_rows($this->statement); 46 | } 47 | 48 | public function bindValue($param, $val, $ignore = null) 49 | { 50 | $this->params[$param] = $val; 51 | } 52 | 53 | public function execute($ignore = null) 54 | { 55 | odbc_execute($this->statement, $this->params); 56 | $this->params = []; 57 | } 58 | 59 | public function fetchAll($how = NULL, $class_name = NULL, $ctor_args = NULL) 60 | { 61 | $records = []; 62 | while ($record = $this->fetch()) { 63 | $records[] = $record; 64 | } 65 | return $records; 66 | } 67 | 68 | public function fetch($option = null, $ignore = null, $ignore2 = null) 69 | { 70 | return odbc_fetch_array($this->statement); 71 | } 72 | } -------------------------------------------------------------------------------- /src/ODBCProcessor.php: -------------------------------------------------------------------------------- 1 | getConnection()->insert($sql, $values); 22 | 23 | $id = $this->getLastInsertId($query, $sequence); 24 | 25 | return is_numeric($id) ? (int)$id : $id; 26 | } 27 | 28 | /** 29 | * @param Builder $query 30 | * @param null $sequence 31 | * @return mixed 32 | */ 33 | public function getLastInsertId(Builder $query, $sequence = null){ 34 | return $query->getConnection()->getPdo()->lastInsertId(); 35 | } 36 | } 37 | -------------------------------------------------------------------------------- /src/ODBCServiceProvider.php: -------------------------------------------------------------------------------- 1 | app->resolving('db', function ($db) { 19 | /** @var DatabaseManager $db */ 20 | $db->extend('odbc', function ($config, $name) { 21 | $pdoConnection = (new ODBCConnector())->connect($config); 22 | $connection = new ODBCConnection($pdoConnection, $config['database'], isset($config['prefix']) ? $config['prefix'] : '', $config); 23 | return $connection; 24 | }); 25 | }); 26 | } 27 | 28 | /** 29 | * Bootstrap the application events. 30 | * 31 | * @return void 32 | */ 33 | public function boot() 34 | { 35 | Model::setConnectionResolver($this->app['db']); 36 | Model::setEventDispatcher($this->app['events']); 37 | } 38 | } 39 | --------------------------------------------------------------------------------