mercredi 28 janvier 2015

PHP if condition for adding and removing without completely rebuilding

I'm creating a PostgreSQL table column management tool to quickly add, remove and rearrange columns in tables. Thus far it is working great in simple scenarios where I simply append a column to the end or remove an existing column. Each of my if conditions call other functions to append a new column, delete an existing column or to rebuild the table as a whole; these functions work great as far as I can currently test.


How do I create an if condition where I'll call both the append function and the delete function instead of the rebuild function?


I have several arrays to help me determine the logic though I'm having trouble getting it worked out. Here are the arrays of column names and again in this scenario I should not be calling the rebuild function, need to call the append function and the delete function.


In this scenario all I'm going to do is delete the column id_2 and add the column id_4 so there is no need to rearrange the order or columns.


$columns_db - The columns as they exist in the PostgreSQL database.



Array
(
[0] => id
[1] => id_1
[2] => id_2
[3] => id_3
)


$columns_updated - The columns in the database that we're going to keep.



Array
(
[0] => id
[1] => id_2
[2] => id_3
)


$columns_submitted - The final way the columns should appear when all is done,



Array
(
[0] => id
[1] => id_2
[2] => id_3
[3] => id_4
)


$columns_slice



$columns_slice = array_slice($columns_submitted,0,(count($columns_updated) - 1),true);

Array
(
[0] => id
[1] => id_2
)


$columns_remove - The column(s) that will be dropped.



Array
(
[1] => id_1
)


$columns_append - The column(s) that will be created.



Array
(
[id_4] => bigint
)

Aucun commentaire:

Enregistrer un commentaire