WordPress.org

Make WordPress Core

Opened 12 years ago

Closed 11 years ago

Last modified 9 years ago

#2862 closed enhancement (wontfix)

SQL formatting method for WPDB

Reported by: DevaSatyam Owned by:
Milestone: Priority: normal
Severity: minor Version:
Component: General Keywords:
Focuses: Cc:

Description

Added a BuildSql method to wpdb. It works, more or less, like a sprintf but oriented towards SQL and WordPress. Escape character is ? followed by a letters indicating the type of data to be inserted and some options. It includes a ?t which adds the contents of $table_prefix. After the first argument, the SQL statement with the ?xxx placeholders, there can be any number of arguments which are inserted into the string as indicated by the datatype and options.

echo BuildSql('Insert into ?ttable (?s,?ns,?mi,?d,?ni,?i)','Something','',5,time(),0,null);

will return:

Insert into wp_table ('Something',null,5,'2006-05-15',null,0)

It contains a PhpDocumenter-style heading with all the options. I was not able to get this version of WP working so I copied the version I'm using on my production site and made some minor modifications to make it self-contained (not dependent on some other functions of mine) which I was unable to test, sorry.

Attachments (1)

wpdb_BuildSql.diff (7.0 KB) - added by DevaSatyam 12 years ago.
patch file

Download all attachments as: .zip

Change History (6)

@DevaSatyam
12 years ago

patch file

#1 @DevaSatyam
12 years ago

  • Type changed from defect to enhancement

#2 @ketsugi
12 years ago

What about a date-time ("Y-m-d H:i:s") format, since that seems to be used pretty heavily in the WP database tables?

#3 @DevaSatyam
12 years ago

You are absolutely right, Ketsugi. In none of my applications I found any need to track time with any precision beyond days and in what I added to my instalation of WP I didn't use that much precision either. According to SQL, if a time part is indicated for a date field, it will be ignored, so the 'd' format could include a time part and it will serve for date, datetime and timestamp datatypes.

What to do with time and year fields? A Y could be used to indicate a year value, but it could also be indicated as a plain integer and mySql will take it, so there is no need to worry about that one. Then, only the time datatype is left out. The 't' is already taken for 'table_prefix'. Does it matter, anyway?

Would you know how to submit a change to a change that has not been accepted yet? I don't want to clutter this thing. If you know the proper way to submit it and want to do it, please feel free.

#4 @rob1n
11 years ago

  • Keywords dev-feedback added

#5 @rob1n
11 years ago

  • Keywords dev-feedback removed
  • Resolution set to wontfix
  • Status changed from new to closed
Note: See TracTickets for help on using tickets.