Re: Insert/select...

Franck Routier <[email protected]> Tue, 05 Mar 2013 09:29:05 +0100
Newsgroups gmane.comp.java.orm.simpleorm
Message-ID <[email protected]>
--------------ms040900050403020109020908
Content-Type: multipart/alternative;
 boundary="------------000102050705030900050604"

--------------000102050705030900050604
Content-Type: text/plain; charset=ISO-8859-1; format=flowed
Content-Transfer-Encoding: quoted-printable

Hi Anthony,

I am very conscious this is the very limit of what an ORM should do. And=20
I know we want to keep SimpleORM simple :-)
But including the feature into the library made it possible to achieve=20
it with very few lines of code...

Another possibility would be to factor out all the query generation part=20
of SimpleORM into a separate package, but I didn't want to break everything=
.
I also still have in mind that if we take that direction, we will want=20
to include some missing functionalities, like more complex query=20
conditions :
- OR
- parenthesis
- maybe SFieldReference based on non equi-join
The latter can be very convinient when using nested-sets based=20
hierarchies (http://en.wikipedia.org/wiki/Nested_set_model) to avoid=20
recursivity and handle hierarchies in sql without relying on proprietary=20
extensions like Oracle's connect by. Currently I use SQuery.rawJoin to=20
handle this kind of hierarchies, which we use a lot in our app...

That said, I realize I've been a bit shortsighted, as I just didn't=20
think of update/delete with the new construct (was not in my use=20
case)... :-(
So maybe I should rename it something like SRawServerUpdate and give it=20
a mode (INSERT, UPDATE, DELETE). Or create different objects... Or just=20
leave it as is until someone needs it...

What's your feeling ?

Regards,
Franck



Le 05/03/2013 00:13, anthony berglas a =E9crit :
>
> Cool.  Good that you called it raw.  I suppose multi-row updates and=20
> deletes would be next on this agenda.
>
>
> I had intended to leave that out of SimpleOrm as you know, but I can=20
> see it is nice to use the OO fields to build the queries.
>
> Anthony
>
> On Mon, Mar 4, 2013 at 8:43 PM, Franck Routier=20
> <[email protected] <mailto:[email protected]>> wrote:
>
>     Hi all,
>
>     I have added a new SInsertSelect construct in SimpleORM.
>
>     This is a low level tool to generate insert into ... select ...
>     style sql based on Metas and SQueryTransient.
>     This has the advantage of:
>     - using object oriented SRecordMeta and SFieldScalar to build your
>     query
>     - delegating the work to the database, which will be the most
>     efficient at this task
>
>     But keep in mind this is a low level trick, closer to jdbc than to
>     an ORM tool.
>     In particular:
>     - this will bypass any validation done by
>     SRecordInstance.onValidateRecord().
>
>
>     So that said, here is how it works... Lets say you have a sales
>     order table, and a stats tables.
>     You can do the following:
>
>     SQueryTransient qryTrs =3D new SQueryTransient(new
>     SQuery<Order>(Order.meta));
>     qryTrs.groupBy(Order.productId).groupBy(Order.month).avg(Order.amount=
).selectRaw("nextval('mysequence')");
>
>     SInsertSelect<Stats> insSel =3D new SInsertSelect(Stats.meta, qryTrs)=
;
>     insSel.addField(Stats.productId).addField(Stats.month).addField(Stats=
.averageOrder).addField(Stats.id);
>
>     session.begin();
>     session.rawInsertSelectNoFlush(insSel);
>     session.commit();
>
>
>     Notice the method is called rawInsertSelectNoFlush to reflect the
>     fact that this is a low level construct.
>     Also notice that the sequence is added using a selectRaw method,
>     and is database dependant (the example will work with Postgresql).
>
>     Hope this can be useful. Any comment is welcome.
>
>     Franck
>
>
>
>
> --=20
>
> Dr Anthony Berglas, [email protected] <mailto:[email protected]>=20
> Mobile: +61 4 4838 8874
> Just because it is possible to push twigs along the ground with ones nose
> does not necessarily mean that that is the best way to collect firewood.
>
>=20


--------------000102050705030900050604
Content-Type: text/html; charset=ISO-8859-1
Content-Transfer-Encoding: quoted-printable

<html>
  <head>
    <meta content=3D"text/html; charset=3DISO-8859-1"
      http-equiv=3D"Content-Type">
  </head>
  <body bgcolor=3D"#FFFFFF" text=3D"#000000">
    <div class=3D"moz-cite-prefix">Hi Anthony,<br>
      <br>
      I am very conscious this is the very limit of what an ORM should
      do. And I know we want to keep SimpleORM simple :-)<br>
      But including the feature into the library made it possible to
      achieve it with very few lines of code...<br>
      <br>
      Another possibility would be to factor out all the query
      generation part of SimpleORM into a separate package, but I didn't
      want to break everything.<br>
      I also still have in mind that if we take that direction, we will
      want to include some missing functionalities, like more complex
      query conditions :<br>
      - OR<br>
      - parenthesis<br>
      - maybe SFieldReference based on non equi-join<br>
      The latter can be very convinient when using nested-sets based
      hierarchies (<a class=3D"moz-txt-link-freetext" href=3D"http://en.wik=
ipedia.org/wiki/Nested_set_model">http://en.wikipedia.org/wiki/Nested_set_m=
odel</a>) to
      avoid recursivity and handle hierarchies in sql without relying on
      proprietary extensions like Oracle's connect by. Currently I use
      SQuery.rawJoin to handle this kind of hierarchies, which we use a
      lot in our app...<br>
      <br>
      That said, I realize I've been a bit shortsighted, as I just
      didn't think of update/delete with the new construct (was not in
      my use case)... :-(<br>
      So maybe I should rename it something like SRawServerUpdate and
      give it a mode (INSERT, UPDATE, DELETE). Or create different
      objects... Or just leave it as is until someone needs it...<br>
      <br>
      What's your feeling ?<br>
      <br>
      Regards,<br>
      Franck<br>
      <br>
      <br>
      <br>
      Le 05/03/2013 00:13, anthony berglas a &eacute;crit&nbsp;:<br>
    </div>
    <blockquote
cite=3D"mid:[email protected].=
com"
      type=3D"cite">
      <span style=3D"display:none">&nbsp;</span>
=20=20=20=20=20=20
          <div id=3D"ygrp-text">
            <p>Cool. &nbsp;Good that you called it raw. &nbsp;I suppose mul=
ti-row
              updates and deletes would be next on this agenda.</p>
            <div><br>
            </div>
            <div>I had intended to leave that out of SimpleOrm as you
              know, but I can see it is nice to use the OO fields to
              build the queries.</div>
            <div><br>
            </div>
            <div>Anthony<br>
              <br>
              <div class=3D"gmail_quote">On Mon, Mar 4, 2013 at 8:43 PM,
                Franck Routier <span dir=3D"ltr">&lt;<a
                    moz-do-not-send=3D"true"
                    href=3D"mailto:[email protected]"
                    target=3D"_blank">[email protected]</a>&gt;</spa=
n>
                wrote:<br>
                <blockquote class=3D"gmail_quote" style=3D"border-left:1px
                  #ccc solid;">Hi all,<br>
                  <br>
                  I have added a new SInsertSelect construct in
                  SimpleORM.<br>
                  <br>
                  This is a low level tool to generate insert into ...
                  select ... style sql based on Metas and
                  SQueryTransient.<br>
                  This has the advantage of:<br>
                  - using object oriented SRecordMeta and SFieldScalar
                  to build your query<br>
                  - delegating the work to the database, which will be
                  the most efficient at this task<br>
                  <br>
                  But keep in mind this is a low level trick, closer to
                  jdbc than to an ORM tool.<br>
                  In particular:<br>
                  - this will bypass any validation done by
                  SRecordInstance.onValidateRecord().<br>
                  <br>
                  <br>
                  So that said, here is how it works... Lets say you
                  have a sales order table, and a stats tables.<br>
                  You can do the following:<br>
                  <br>
                  SQueryTransient qryTrs =3D new SQueryTransient(new
                  SQuery&lt;Order&gt;(Order.meta));<br>
                  qryTrs.groupBy(Order.productId).groupBy(Order.month).avg(=
Order.amount).selectRaw("nextval('mysequence')");<br>
                  <br>
                  SInsertSelect&lt;Stats&gt; insSel =3D new
                  SInsertSelect(Stats.meta, qryTrs);<br>
                  insSel.addField(Stats.productId).addField(Stats.month).ad=
dField(Stats.averageOrder).addField(Stats.id);<br>
                  <br>
                  session.begin();<br>
                  session.rawInsertSelectNoFlush(insSel);<br>
                  session.commit();<br>
                  <br>
                  <br>
                  Notice the method is called rawInsertSelectNoFlush to
                  reflect the fact that this is a low level construct.<br>
                  Also notice that the sequence is added using a
                  selectRaw method, and is database dependant (the
                  example will work with Postgresql).<br>
                  <br>
                  Hope this can be useful. Any comment is welcome.<span
                    class=3D"HOEnZb"><font color=3D"#888888"><br>
                      <br>
                      Franck<br>
                      <br>
                    </font></span></blockquote>
              </div>
              <br>
              <br clear=3D"all">
              <div><br>
              </div>
              -- <br>
              <p>
              </p>
              <p>Dr Anthony Berglas, <a moz-do-not-send=3D"true"
                  href=3D"mailto:[email protected]" target=3D"_blank">ant=
[email protected]</a>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;
                Mobile: +61 4 4838 8874<br>
                Just because it is possible to push twigs along the
                ground with ones nose<br>
                does not necessarily mean that that is the best way to
                collect firewood.</p>
            </div>
          </div>
=20=20=20=20=20=20=20=20=20=20
=20=20=20=20=20=20
      <!-- end group email -->
    </blockquote>
    <br>
  </body>
</html>

--------------000102050705030900050604--

--------------ms040900050403020109020908
Content-Type: application/pkcs7-signature; name="smime.p7s"
Content-Transfer-Encoding: base64
Content-Disposition: attachment; fil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