mongo-sql icon indicating copy to clipboard operation
mongo-sql copied to clipboard

Insert sub query

Open jrf0110 opened this issue 12 years ago • 2 comments

http://stackoverflow.com/questions/4069718/postgres-insert-if-does-not-exist-already/13342031#13342031

jrf0110 avatar Aug 13 '13 02:08 jrf0110

This is going to require a bit of re-working. Perhaps we should make it a bit more semantic.

Currently, we only support simple inserts ("colA", "colB") values ($1, $2). The values helper accepts an object whose keys are used as column names and values are used as... values.

The poses a problem for someone wanting to use a sub-query as the values. Sub-queries are objects with no way to distinguish them from normal values objects as they currently are.

If we break up the values helper into two helpers and change the semantics of the insert statement slightly, we can easily implement this. Here's how it could work:

// Standard insert
{
  type: 'insert'
, table: 'users'
, columns: ['name', 'email', 'password']
, values: ['Tom', '[email protected]', 'alsdkjfaldsfa']
}

// Sub-select
{
  type: 'insert'
, table: 'users'
, columns: ['name', 'email', 'password']
, values: {
    type: 'select'
  , table: 'consumers'
  , limit: 1
  } 
}

I think the standard insert has lost some semantic meaning here as you can no longer access the values in a hash-style lookup. Also, this requires some finagling to objects to fit this structure. So, perhaps we keep values the way it is and provide the new columns helper and just add in the expression helper to the insert.

{
  type: 'insert'
, table: 'users'
, columns: ['name', 'email', 'password']
, expression: {
    type: 'select'
  , table: 'consumers'
  , limit: 1
  } 
}

jrf0110 avatar Aug 13 '13 03:08 jrf0110

Ok yeah I think I'm gonna go with the latter. But I'm gonna sleep on it

jrf0110 avatar Aug 13 '13 03:08 jrf0110