Skip to content

Instantly share code, notes, and snippets.

@Xliff
Last active August 17, 2022 01:31
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save Xliff/c78fb770c8b1d23d2b7be0bdb007e9c6 to your computer and use it in GitHub Desktop.
Save Xliff/c78fb770c8b1d23d2b7be0bdb007e9c6 to your computer and use it in GitHub Desktop.
How's this for SQL Code?

Wouldn't it be nice if you could write your queries like this?

  #| SELECT DISTINCT date_format(`last-updated`, '%i') minute
  #| FROM instances
  #| ORDER BY 1
  method getReportingMinutes (Str $instance-id) is query { }

Or this...

  #| -- getTicketReportData
  #| SELECT
  #|   t.id                                            `number`,
  #|   t.post_date                                     `date`,
  #|   t.post_title                                    `title`,
  #|   ts.meta_value                                   `ticket-status`,
  #|   a.user_login                                    `agent`,
  #|   d.name                                          `department`,
  #|   STR_TO_DATE(cd.meta_value, '%Y-%m-%d %H:%i:%s') `closed-on`
  #| FROM       wp_posts              t
  #| INNER JOIN wp_postmeta           aa ON aa.post_id          = t.id
  #| INNER JOIN wp_users              a  ON a.id                = aa.meta_value
  #| LEFT  JOIN wp_term_relationships da ON da.object_id        = t.id
  #| INNER JOIN wp_term_taxonomy      tt ON tt.term_taxonomy_id = da.term_taxonomy_id
  #| LEFT  JOIN wp_terms              d  ON d.term_id           = da.term_taxonomy_id
  #| INNER JOIN wp_postmeta           ts ON ts.post_id          = t.id
  #| LEFT  JOIN wp_postmeta           cd ON cd.post_id          = t.id
  #| WHERE
  #| 	 t.post_type  = 'ticket'
  #|   AND
  #|   aa.meta_key  = '_wpas_assignee'
  #|   AND
  #|   tt.taxonomy = 'department'
  #| 	 AND
  #|   ts.meta_key  = '_wpas_status'
  #|   AND
  #|   cd.meta_key  = '_ticket_closed_on'
  method getTicketReportData ($from, $to, :$extra-logic) is query<hash> { }

Also, note that parameters are allowed, but they must match up with the parameter marker ?:

  #| SELECT id FROM lightsail_instances WHERE data->"$.name" = ?
  method getLightsailInstanceByName ($n)
    is query<hash>
  { }

I am currently developing a system that does just that, and the two queries above are working examples!

@Xliff
Copy link
Author

Xliff commented Aug 17, 2022

Might this make more sense?

  #| SQL«
  #|   SELECT DISTINCT date_format(`last-updated`, '%i') minute
  #|   FROM instances
  #|   ORDER BY 1
  #| »
  method getReportingMinutes (Str $instance-id) is query { }

This has the extra advantage for allowing different types of queries, ala:

#| DATALOG«
#| (xt/q
#|   (xt/db node) (1)
#|     '{:find [p1] (2)
#|         :where [[p1 :name n]
#|           [p1 :last-name n]
#|           [p1 :name name]]
#|    :in [name]}
#|   "Ivan") (3)
#| »
method myDatalogQuery is query { }

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment