SkyPHP / skyphp

PHP5 Framework
http://switchbreak.com/skyphp
17 stars 38 forks source link

Multi-Insert Idea #17

Open stanistan opened 12 years ago

stanistan commented 12 years ago

Right now, we have this insert structure:

<?php 
aql::insert($table_name, $field_value_pairs);

And no way to quickly insert multiple records quickly, the only way is to loop through an array of fields and run an individual aql::insert, which also does a select to return the inserted row.

Proposal

<?php
$table_name = 'artist';

$fields = array(
    array(
        'name' => 'Pink Floyd'
    ),
    array(
        'name' => 'Metallica',
        'year_started' => 'Not sure'
    ),
    array(
        'name' => 'Sleepytime Gorilla Museum',
        'genre' => 'weird' // not actual genre schema, 
    ),
    array(
        'name' => 'Radiohead'
    ),
    // etc
);

aql::multi_insert($table_name, $fields);

This would figure out the minimum number of insert queries for field-data pairs given and insert them in a transaction. If all of the fields are the same, then this would be minimized to one query which would be much faster than the current alternative.

If aql::insert() contains a multi-dimensional array, multi_insert() would be used instead.

Note: this would not return an array of rows inserted.

zgr024 commented 12 years ago

multi_insert() should return an array of inserted id's