Quintiq file version 2.0
|
#parent: #root
|
Method GetOrCreate (
|
String table,
|
String keyNameTuple,
|
String nonKeyNameTuple,
|
String keyValueTuple,
|
String nonKeyValueTuple
|
) as Number
|
{
|
Description: 'Maintain by R&D'
|
TextBody:
|
[*
|
// jasperb Feb-21-2012 (created)
|
// danr June-6-2012 (changed for WriteToFile in particular)
|
|
/*
|
Input arguments:
|
* table: e.g. benchmark or setup
|
* nametuple: comma-seperated column titles
|
* keyValueTuple: comma-separated values, these *are* used for matching (avoiding duplicates)
|
* nonkeyValueTuple: : comma-separated values, not used for matching
|
|
If keyValueTuple = "" then unique entries will always be created
|
|
Writing to file makes CSV's (using Benchmark::Delimiter (currently '$') as separator), one for each table.
|
First column of "table" is always the id.
|
Following columns are always the key columns, after that the non-key ones.
|
*/
|
|
result := -1;
|
|
nameTuple := '';
|
valueTuple := '';
|
|
if( keyValueTuple <> "" and nonKeyValueTuple <> "" )
|
{
|
nameTuple := keyNameTuple + "," + nonKeyNameTuple;
|
valueTuple := keyValueTuple + "," + nonKeyValueTuple;
|
}
|
else
|
{
|
nameTuple := keyNameTuple + nonKeyNameTuple;
|
valueTuple := keyValueTuple + nonKeyValueTuple
|
}
|
|
if( this.WriteToFile() and this.WriteToDB() )
|
{
|
error( 'both WriteToFile() = true and WriteToDB() = true; cant deal with that' );
|
}
|
|
if( this.WriteToFile() )
|
{
|
OS::CreateDirectory( this.Directory() );
|
|
filename := this.Directory() + '/' + table + '.csv'
|
|
file := OSFile::Construct();
|
file.Open( filename, 'ReadWrite', true );
|
|
searchFor := BenchmarkUtilities::FormatCSV( keyValueTuple );
|
matchID := 0; maxID := 0; count := 0;
|
lineID := 0; lineRest := '';
|
|
while( not file.IsEOF() and matchID = 0 )
|
{
|
line := file.ReadTextLine();
|
count++;
|
|
if( count > 1 )
|
{
|
// We skip the header line, if it exists
|
BenchmarkUtilities::SplitCSVLine( line, lineID, lineRest );
|
// Keep track of max. ID that occurs
|
maxID := maxvalue( lineID, maxID );
|
// See if this is the line we're looking for
|
if( keyValueTuple <> '' and lineRest.FindString(searchFor, 0 ) = 0 )
|
{
|
matchID := lineID;
|
}
|
}
|
}
|
|
|
if( count = 0 )
|
{
|
// file is empty
|
file.WriteTextLine( BenchmarkUtilities::FormatCSV( table + 'id,' + nameTuple ) );
|
}
|
|
result := -1;
|
if( matchID = 0 )
|
{
|
// No match, write new one
|
newID := maxID + 1;
|
file.WriteTextLine( BenchmarkUtilities::FormatCSV( [String] newID + ',' + valueTuple ) );
|
result := newID;
|
}
|
else
|
{
|
// match
|
result := matchID;
|
}
|
file.Close();
|
}
|
|
if( this.WriteToDB() )
|
{
|
idname := table + "id";
|
|
selectquery := "SELECT " + idname + " FROM " + table + " WHERE (" + keyNameTuple + ")=(" + keyValueTuple + ") ORDER BY " + idname;
|
queryresult := this.FlatQuery( selectquery );
|
|
if( queryresult.NrRows() = 0 )
|
{
|
insertquery := "INSERT INTO " + table + " (" + nameTuple + ") VALUES (" + valueTuple + ")";
|
queryresult := this.FlatQuery( insertquery );
|
|
infoquery := "SELECT LAST_INSERT_ID()";
|
queryresult := this.FlatQuery( infoquery );
|
|
if( queryresult.NrRows() = 0 )
|
{
|
error('SELECT LAST_INSERT_ID() returns nothing');
|
}
|
}
|
else if( queryresult.NrRows() > 1 )
|
{
|
info('WHOA! More than one matching result found while updating table ' + table);
|
}
|
|
result := queryresult.Row( 0 ).Field( 0 ).AsNumber();
|
}
|
|
if( not this.WriteToFile() and not this.WriteToDB() )
|
{
|
result := 0;
|
}
|
|
return result;
|
*]
|
}
|