samedi 2 juillet 2016

Inserting and updating data into a database under certain terms

I have an xml from which I have to get the data: stations with belonging coordinates and type of the station, and measurments like temperature, humidity etc. that are made on that station, and put all of that in the database. Xml file updates every 1 hour. Measurments are not observered every hour on the same station. So for example I can have:

5 am - Station1 - Measurments1, Station2 - Measurments2, Station3 - Measurments3 etc.
6 am - Station 3 - Measurments3, Station4 - Measurments4, Station5 - Measurments5 etc.

My database looks like this:

station       measurment         view: vwstationmeasurment
-------       ----------         -------------------------
gid (PK)      gid (PK)           gid
station_name  time               station
station_type  temperature        time
geometry      station_id (FK)    temperature

I got the data from xml as string (I can also produce array from all of the data from xml) and I have inserted all the data (I am leaving out part of the code that gets data from xml):

foreach ($xml as $position => $row) {

        $time = $row['time'];
        $station_name = $row['name_station'];
        $longitude = $row['longitude'];
        $latitude = $row['latitude'];
        $station_type = $row['type_station'];
        $temperature = $row['temperature'];

        try {
                // connect to the database
                $dbh = new PDO('pgsql:host='.DB_HOST. ';port=' .DB_PORT. ';dbname=' .DB_NAME, DB_USER, DB_PASS);

                // start transaction
                $dbh->beginTransaction();

                // prepare first SQL query, execute it, pick up the last ID and print msg about it
                $sql_station = "INSERT INTO station (geom, station_name, station_type) VALUES (ST_SetSRID(ST_MakePoint($longitude, $latitude), 4326), '$station_name', '$station_type')";
                $stmt = $dbh->prepare($sql_station);
                $stmt->execute();
                $station_id = $dbh->lastInsertId('station_gid_seq');
                // prepare second SQL query, execute it, pick up the last ID and print msg about it
                $sql_measurment = "INSERT INTO measurment (time, temperature, station_id) VALUES ('$time', '$temperature', '$station_id')";
                $stmt = $dbh->prepare($sql_measurment);
                $stmt->execute();
                $measurment_id = $dbh->lastInsertId('measurment_gid_seq');

                // save transaction
                $dbh->commit();

                // close database connection
                $dbh = null;

        } catch (PDOException $e) {
                // cancel the transaciton if something went wrong and write msg about it
                $dbh->rollBack();
                print "Error!: " . $e->getMessage() . "<br/>";
                die();
        }
}

I had troubles with binding parameteres in the first query where coordinates have to be inserted into a postgis, after searching a little bit I got impression it is not possible to do the PDO with this kind of statement so I just let it go. I guess the complete purpose of PDO is not done here, but it works.

So what I thought that it could be the main concept of soloving the problem is: - if tables: "station" and "measurments" are empty, insert all data - if certain station in table "station" doesnt exists insert data for it - if time for measurments has changed update data in table measurment

Problem is that I dont know how to loop everything, more precise how to get the station from xml and station from database for the purpose of comparasion in IF statement, either if I take data as a string or as an array. I wanted to do something like this:

try {
            $sql_vwstationmeasurment_select = "SELECT * FROM vwstationmeasurment";
            $stmt_vwstationmeasurment_select = $dbh->prepare($sql_vwstationmeasurment_select);
            $stmt_vwstationmeasurment_select->execute();
            $result_vwstationmeasurment_select = $stmt_vwstationmeasurment_select->fetchAll(PDO::FETCH_ASSOC);

            $result_vwstationmeasurment_select_array = array ();
            foreach($result_vwstationmeasurment_select as $row) {
                    $station_name_db = $row['station_name'];
                    $time_db = $row['time'];

                    if ($station_name_db != $station_name OR empty($station_name_db)){

                            $sql_station_insert = "INSERT INTO station (geom, station_name, station_type) VALUES (ST_SetSRID(ST_MakePoint($longitude, $latitude), 4326), '$station_name', '$station_type')";
                            $stmt_station_insert = $dbh->prepare($sql_station_insert);
                            $stmt_station_insert->execute();
                            $station_id = $dbh->lastInsertId('station_gid_seq');

                            $result_station_insert = $stmt_station_insert->fetchAll(PDO::FETCH_ASSOC);

                            $sql_measurment = "INSERT INTO measurment (time, temperature)  VALUES ('$time', '$temperature')";
                            $stmt_measurment = $dbh->prepare($sql_measurment);
                            $stmt_measurment->execute();
                            $measurment_id = $dbh->lastInsertId('measurment_gid_seq');

                    } elseif($time_db != $time){
                             $sql_measurment = "UPDATE measurment SET time = $time, temperature = $temperature",
                    }
                            $result_vwstationmeasurment_select_array[] =($row);
            }

            // save transaction
            $dbh->commit();

                            // close database connection
            $dbh = null;

   } catch (PDOException $e) {
            // cancel the transaciton if something went wrong and write msg about it
            $dbh->rollBack();
            print "Error!: " . $e->getMessage() . "<br/>";
            die();
   }

But this code inserts so many things, I guess because I have foreach loop -> foreach loop.

I was also thinking that I could solove somehow issue with array_data_xml and array_data_db, merge that two arrays and compare if keys and values are equal or different, but this is just theoretical approach, I wouldnt know how to loop that also.

I would really appriciate if someone help me with that!

Aucun commentaire:

Enregistrer un commentaire