Support for Drupal 7 is ending on 5 January 2025—it’s time to migrate to Drupal 10! Learn about the many benefits of Drupal 10 and find migration tools in our resource center.
According to the new DB abstraction layer in D7, drupal_write_record() can be revamp with db_insert() and db_merge() for simpler implementation.
Patch via CVS HEAD, tested with both MySQL and PostgreSQL, create and update node successfully.
Comment | File | Size | Author |
---|---|---|---|
#30 | mw.patch | 6.06 KB | moshe weitzman |
#28 | mw.patch | 6.06 KB | moshe weitzman |
#26 | dbtng-drupal_write_record-1225283053.patch | 5.62 KB | hswong3i |
#23 | drupal_write_record-1223532230.patch | 5.62 KB | hswong3i |
#22 | drupal_write_record-1222944769.patch | 5.57 KB | hswong3i |
Comments
Comment #1
hswong3i CreditAttribution: hswong3i commentedComment #2
hswong3i CreditAttribution: hswong3i commentedComment #3
Dries CreditAttribution: Dries commentedDo we have tests for drupal_write_record()? If not, this would probably be a good time to create some -- or to review/extend/improve the existing ones if necessary.
Comment #4
hswong3i CreditAttribution: hswong3i commentedReroll patch with db_update() but not db_merge(), which should be more suitable.
BTW, it is also known as buggy when working with MySQL, as like as another issue (http://drupal.org/node/299385#comment-980272). The new query builder db_insert() is buggy when working with db_last_insert_id() (just after db_insert()) due to
INSERT DELAYED
. The programming logic of this patch should be correct: when I mask the 'DELAYED' from InsertQuery_mysql(), all cases work fine.Comment #5
hswong3i CreditAttribution: hswong3i commentedReroll patch without db_last_insert_id(), fix the idea of serial field(s), and a bit code cleanup.
Patch via CVS HEAD, tested with MySQL and PostgreSQL, pass both node and user INSERT/UPDATE.
Comment #6
moshe weitzman CreditAttribution: moshe weitzman commentedI code reviewed this and it looks good. If the test suite reports no fails, I'd say this is RTBC.
Comment #7
moshe weitzman CreditAttribution: moshe weitzman commentedComment #8
hswong3i CreditAttribution: hswong3i commentedCatch the bug when working with http://drupal.org/node/371. db_insert() and db_update() can't handle invalid integer and float content, if input value is not in correct type, e.g. input
''
for integer field.For traditional db_query() most int/float will be filtered as correct data type but now we need to handle that manually.
Patch reroll via CVS HEAD, tested with MySQL, simpletetest + node and taxonomy, no error generated :D
Comment #9
moshe weitzman CreditAttribution: moshe weitzman commentedWhy are these two queries so special that they need to type cast integers and float? If thats truly needed, do it inside the DB layer so every query benefits. Please remove that code or explain why it belongs here and not in the DB layer.
Comment #10
hswong3i CreditAttribution: hswong3i commentedPlease checkout CVS HEAD
includes/database/database.inc
function_db_query_process_args()
(line 1824) for more information:Comment #11
moshe weitzman CreditAttribution: moshe weitzman commentedOK, but should we not fix the data before it gets to drupal_write_record()? Why is this the right place to cast?
Comment #12
hswong3i CreditAttribution: hswong3i commentedI have no preference for handling the casting within drupal_write_record(), but else we will need to go though:
Some other potential cases should take in consideration (e.g. http://drupal.org/node/300788, http://drupal.org/node/299385 and http://drupal.org/node/300203), too. Any idea?
Comment #13
moshe weitzman CreditAttribution: moshe weitzman commentedYeah, i guess that would grow this patch beyond what it wants to be.
Comment #14
hswong3i CreditAttribution: hswong3i commentedSorry that we shouldn't apply type cast (!?) within drupal_write_record() internally, or else will generate more error message in simpletest :S
I found that after rollback to previous version, we only buggy in dblog and node, with
sticky
(int) andpromote
(int) input as''
which are invalid. Apply a very minor type casting can solve the problem. I also give a (almost) complete set of simpletest twice and found there is no additional error generated with MySQL.Please try to run simpletest with: block, contact, dblog, node, taxonomy and user, compare result with CVS HEAD. It should now be safe.
Comment #15
catchSurely dblog and node could actually insert ints there instead of empty strings?
Comment #16
moshe weitzman CreditAttribution: moshe weitzman commentedi am seeing a test failure in Forum test related to vocab edit form. All other tests pass.
Comment #17
hswong3i CreditAttribution: hswong3i commented@catch: I add the following code in includes/database/database.inc, run simpletest and check syslog for debug:
@moshe: Yup I catch this bug too. Patch updated with type casting. Tested with MySQL + dblog, node, taxonomy and forum. PostgreSQL seems a bit buggy with node revisions.
Comment #18
moshe weitzman CreditAttribution: moshe weitzman commentedThe patch has no changes to forum module.
Crell mentioned that he thought drupal_write_record should be deprecated now that we have merge queries. He is almost right. I think the only use it has now is to remove items from the supplied object/array that do not belong in the target table. Perhaps we could make a helper function for that and then toss dwr. Could be done in a later issue.
Comment #19
hswong3i CreditAttribution: hswong3i commentedActually I have no preference with drupal_write_record(), but from my point of view it is completely different from db_*() and so not interchangeable.
What the main duty of drupal_write_record() is to check schema definition, e.g. serial? default value? serialized? last insert id? For sure it is much high level than using db_*() directly without any checking and bonus benefit.
On the other hand, in some rear case, e.g. insert query of watchdog, we are not able to use drupal_write_record() because common.inc is not even load in yet. Therefore using db_insert() is a must.
Comment #20
hswong3i CreditAttribution: hswong3i commentedUpdated version. Correctly type casting input values, based on schema definition (only field with not serial and not allow null). Similar as legacy _db_query_callback(). Also update input variable $update as $primary_keys for better naming. So now no longer need to type casting before each call of drupal_write_record().
Patch via CVS HEAD, tested with MySQL. Run parallel simpletest (node, taxonomy, dblog, comment) and coming with identical result (P.S. CVS HEAD is also buggy in simpletest...)
Comment #21
drewish CreditAttribution: drewish commentedjust wanted to weight in that this doesn't affect any of the tests and a visual review of the patch didn't turn up any problems.
Comment #22
hswong3i CreditAttribution: hswong3i commentedUpdated version. Coming with better type cast handling.
Patch via CVS HEAD, tested with MySQL and PostgreSQL. Run parallel simpletest (blog, node, taxonomy, dblog, comment) and coming with identical result.
P.S. Since this patch is just a wrapper of db_insert() and db_update(), the "empty string => NULL, in case of BLOB field" bug for PostgreSQL declared in http://drupal.org/node/316095 will still happening.
Comment #23
hswong3i CreditAttribution: hswong3i commentedCross test with following issues and all looks fine. Should we have more discussion for this issue, or move it as RTBC?
#299385: [DBTNG + BLOB] remap {aggregator_item}.description
#300203: Remap field as BLOB: {boxes}.body
#300788: [DBTNG + BLOB] remap {comments}.comment
#318650: Remap field as BLOB: {menu_router}.access_arguments and {menu_router}.page_arguments
Edit: Moshe changed this message to use our issue number hyperlinks method - [#n].
Comment #24
hswong3i CreditAttribution: hswong3i commentedJust recall that patch from #23 still apply to CVS HEAD, and the hidden PosgreSQL + BLOB bug (#316095: Raise PHP requirement to 5.2.12 for PostgreSQL only) is belongs to PHP pdo_pgsql but not this patch.
Comment #25
hswong3i CreditAttribution: hswong3i commentedSince PosgreSQL + BLOB bug (#316095: Raise PHP requirement to 5.2.12 for PostgreSQL only) is now coming with solution, so change of this patch is now safe. Even combine with #300802: [DBTNG + BLOB]: remap {node_revisions}.body and {node_revisions}.teaser is now also safe for PostgreSQL. All path should be now cleaned so this patch can commit without any problem.
Maybe we still need some more discussion, or is time for this issue to be mark as RTBC? Patch from #23 still apply clean to CVS HEAD.
Comment #26
hswong3i CreditAttribution: hswong3i commentedPatch reroll via CVS HEAD.
Comment #27
Dries CreditAttribution: Dries commentedI've asked Moshe to give this a review. He is original author of drupal_write_record() and better understands some of the subtleties of that patch.
Comment #28
moshe weitzman CreditAttribution: moshe weitzman commentedI just fixed up some code comments. You can see that the last patch passed all 7000 unit tests. I did some re-testing on this patch and it still looks good. Thanks for modernizing for this function.
Comment #29
keith.smith CreditAttribution: keith.smith commented+ // For inserts, populate defaults from schema if not already provided
No ending period.
Comment #30
moshe weitzman CreditAttribution: moshe weitzman commentedRerolled for missing period. Thanks.
Comment #31
Dries CreditAttribution: Dries commentedCommitted to CVS HEAD. Thanks! :)
Comment #32
Anonymous (not verified) CreditAttribution: Anonymous commentedAutomatically closed -- issue fixed for two weeks with no activity.