Code Monkey home page Code Monkey logo

database's People

Stargazers

 avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar

Watchers

 avatar  avatar  avatar  avatar

database's Issues

select an unescaped field (e.g. a function)

I came across a problem using functions with this component. The SQL query I'd like to execute:

SELECT md5(random()::text || clock_timestamp()::text)::uuid

When using the component I end up with the following:

$result = $query
            ->table('pg_language')
            ->fields(array(
                'md5(random()::text || clock_timestamp()::text)::uuid'
            ))
            ->execute();

// Query: SELECT "md5(random()::text || clock_timestamp()::text)::uuid"
// The field will be handled as a column name and ends up in an error

I created an ugly workaround to achieve my goal:

$result = $query
            ->table('pg_language')
            ->fields(array(
                'lanname", md5(random()::text || clock_timestamp()::text)::uuid AS UUID, "lanowner'
            ))
            ->execute();
// Query: SELECT "lanname", md5(random()::text || clock_timestamp()::text)::uuid AS UUID, "lanowner" FROM "pg_language"

I recommend to add a new function like fields but name it unescapedFields or something.

Weird escaping necessary for schemas in PostgreSQL

Assume you want to query mytable of the following database structure:

mydatabase
   ⌙-> myschema
          ⌙-> mytable

A general query would look like this (given that you selected the database):

SELECT * FROM "myschema"."mytable";

Translating it to the Database component:

$gateways = $query
    ->table('myschema.mytable');

/*
execute():

Fatal error: Uncaught exception 'PDOException' with message 'SQLSTATE[42P01]: Undefined table: 7 ERROR: relation "myschema.mytable" does not exist LINE 1: SELECT * FROM "myschema.mytable" ^' in ...

parse():

object(PHPixie\Database\Type\SQL\Expression)#51 (2) { ["sql"]=> string(28) "SELECT * FROM "myschema.mytable"" ["params"]=> array(0) { } }

SQL Query:
SELECT * FROM "myschema.mytable" // invalid
*/

$gateways = $query
    ->table('"myschema"."mytable"');

/*
execute():

Fatal error: Uncaught exception 'PDOException' with message 'SQLSTATE[42601]: Syntax error: 7 ERROR: zero-length delimited identifier at or near """" LINE 1: SELECT * FROM ""myschema"."mytable"" ^' in 

parse():

object(PHPixie\Database\Type\SQL\Expression)#51 (2) { ["sql"]=> string(32) "SELECT * FROM ""myschema"."mytable""" ["params"]=> array(0) { } }

SQL Query:
SELECT * FROM ""myschema"."mytable"" // invalid
*/

For a correct escaping I have to write the table like this: myschema"."mytable because the component just adds a double quote at the beginning and at the end of the table definition.

$query = $connection->selectQuery();
$gateways = $query
    ->table('myschema"."mytable');

parse():

object(PHPixie\Database\Type\SQL\Expression)#51 (2) { ["sql"]=> string(30) "SELECT * FROM "myschema"."mytable"" ["params"]=> array(0) { } }

SQL Query:
SELECT * FROM "myschema"."mytable" // valid

I suggest to escape the table like this:

  • explode table by . (dot)
  • escape each component
  • implode components by . (dot)

This would also work for MySQL, where you can query by mydatabase.mytable

Documentation for INSERT query is wrong

If you create a INSERT query like it's described in the README.md it won't work:

// Inserting
$insertQuery = $connection->insertQuery();
$insertQuery->data(
    'id'    => 1,
    'title' => 'Hello'
)->execute();

It seems like the array() was forgotten.

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.