Code Monkey home page Code Monkey logo

yesql-php's Introduction

yesql-php

This is a clone of the wonderful yesql library from clojure. The idea is to have a seperate sql file for queries, which you can then access as methods on a class.

I built for fun on a Friday, liked it a lot and now we are using it where I work. So I guess it's production ready.

Installation

Use composer to require:

"nulpunkt/yesql-php": "^1"

Examples!

You need to make a repository of queries:

$pdo = new PDO($host, $user, $pass); // Fill in the blanks
$r = new Nulpunkt\Yesql\Repository($pdo, "my-queries/queries.sql");

Fetching many rows

in queries.sql we can put:

-- name: getAllRows
-- This will fetch all rows from test_table
select * from test_table;

which will allow us to call

$r->getAllRows();

Inserting a row

A database without rows is not of much use, lets insert some data:

-- name: insertRow(thing)
insert into test_table (something) values (:thing)
// returns the insertId
$r->insertRow('a thing');

As default, yesql will simply bind all params passed to the called function, to the query associated with it. We'll see how to make mappers further down.

Updating a row

Maybe we need to fix some exsisting data

-- name: updateRow(id, thing)
update test_table set something = :thing where id = :id
// returns the number of rows touched by the update
$r->updateRow(3, 'fixed thing');

Fetching a single row

yesql-php support different modlines, lets say we know we only need to get one row:

-- name: getById(id) oneOrMany: one
select * from test_table where id = :id;
// Fetches one row with id 3
$r->getById(3);

Fetching and mapping rows in one go

Maybe we want to return a modified version of the row. By specifying a rowFunc, we can have a function called, on every row returned:

-- name: getMappedById(id) oneOrMany: one rowFunc: MyObject::mapRow
select * from test_table where id = :id
class MyObject {
  public static function mapRow($r) {
    return ['id' => $r['id'], 'ohwow' => $r['something']];
  }
}
// return one row, with keys id and ohwow
$r->getMappedById(3);

Mapping rows to objects

Sometimes an object is want you want, rowClass got your back:

-- name: getObjectById(id) oneOrMany: one rowClass: MyObject
select * from test_table where id = :id
class MyObject {
}
// return one row, which is an instance of MyObject with id and something set
$r->getObjectById(3);

Mapping data on the way in

We may need to map our domain objects to be able to insert them into the database.

-- name: insertObject inFunc: MyObject::toRow
insert into test_table (id, something) values (:id, :something)
class MyObject {
  // $i will be the arguments passed to insertObject
  public static function toRow($i, $o) {
    return ['id' => $i, 'something' => $o->something];
  }
}
$o = new MyObject;
$r->insertObject($i, $o)

yesql-php's People

Contributors

nulpunkt avatar terales avatar

Stargazers

 avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar

Watchers

 avatar  avatar  avatar  avatar  avatar  avatar  avatar

yesql-php's Issues

Indicate whether it's ready for use in production

Hello! Thanks a lot for open sourcing your port!

In the README you're stating:

I'm trying to build a clone of the wonderful yesql library from clojure. The idea is to have a seperate sql file for queries, which you can then access as methods on a class.

I'm a bit confused by this statement. Is it ready for use in production? Can you specify this in the README, please?

PHP 8.3.2 : deprected warnings

Thank you for this projet I use it on some projets with success!

I inform you, that on a new project, with PHP 8.3.2, I see this info level messages about deprecated use in your code:

[PHP-CGI    ] [16-Feb-2024 12:49:23 UTC] [info] Deprecated: Creation of dynamic property Nulpunkt\Yesql\Statement\Select::$rowClass is deprecated
[PHP-CGI    ] [16-Feb-2024 12:49:23 UTC] [info] Deprecated: Creation of dynamic property Nulpunkt\Yesql\Statement\MapInput::$argNames is deprecated
[PHP-CGI    ] [16-Feb-2024 12:49:23 UTC] [info] Deprecated: Creation of dynamic property Nulpunkt\Yesql\Statement\MapInput::$inFunc is deprecated
Technology Version
PHP 8.3.2
Symfony 6.4
Yesql-php 1.5

I hope this will be useful.

Add support for named parameters

We have several queries we want to refactor but right now it's scary because of strict order of the parameters. I would love to add support of the names parameters like this:

-- name: getPaymentsExample
SELECT * FROM payments AS p WHERE p.from = :from AND p.to = :to

<?php
// get payments
$repository->getPaymentsExample([':from' => $from. ':to' => $to]);

Do you like the idea?
Would you accept a PR for this?

Add titles to the examples in the README

As @nulpunkt asked in #5 (comment):

The readme is getting longer, so it would be cool with ideas on how to improve it!

I don't mind long README, but I see it as a very useful feature. With long README you can search on the page with a browser and without any code. Consider example:
https://github.com/avajs/ava

The only thing we need to have a useful long README is to add the Contents section. To make it more readable we need to add titles to the examples.

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    ๐Ÿ–– Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. ๐Ÿ“Š๐Ÿ“ˆ๐ŸŽ‰

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google โค๏ธ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.