Skip to content

Commit

Permalink
Remaining API updates
Browse files Browse the repository at this point in the history
  • Loading branch information
benkeen committed Jan 29, 2015
1 parent 63aa811 commit 417104e
Show file tree
Hide file tree
Showing 3 changed files with 84 additions and 35 deletions.
1 change: 1 addition & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -49,6 +49,7 @@ contributors agree that all code is released under this license.

In addition to the many folks who submit bug reports, a big thanks to the following for their help extending the script:

- [Daragh Courtney](https://github.com/daraghc) - [Data Generator Vagrant repo](https://github.com/benkeen/generatedata-vagrant)
- [Zhao Yang](https://github.com/jptiancai) - Chinese language file (3.1.4)
- [Anton Nizhegorodov](https://github.com/an1zhegorodov) - Batch size SQL export option (3.1.3)
- Zeeshan Shaikh - PAN, PIN, CVV, Track 1 and 2 Data Types (3.1.1)
Expand Down
31 changes: 21 additions & 10 deletions plugins/exportTypes/SQL/README.md
Original file line number Diff line number Diff line change
Expand Up @@ -9,17 +9,26 @@ Using the Data Generator interface you can just select whatever options you want
but if you're using the API, these are the settings you'll be interested in.

- `tableName`: the string name of the database table to insert/update the data. *required*
- `databaseType`: a string. One of: `MySQL`, `Postgres`, `SQLite`, `Oracle`, `MSSQL`
- `createTable`: a boolean. If this is set to true, the generated output will contain a `CREATE TABLE` query at the start.
- `dropTable`: a boolean. If this is set to true, the first generated query will be a safe DROP TABLE to ensure the
table doesn't already exist prior to the INSERT queries.
- `encloseWithBackquotes`: a boolean. Whether column and table names will be enclosed in backquotes.
- `databaseType`: a string. One of: `MySQL`, `Postgres`, `SQLite`, `Oracle`, `MSSQL`. *required*
- `createTable`: a boolean, defaults to `true`. If this is set to true, the generated output will contain a
`CREATE TABLE` query at the start.
- `dropTable`: a boolean, defaults to `false`. If this is set to true, the first generated query will be a safe
DROP TABLE to ensure the table doesn't already exist prior to the INSERT queries.
- `encloseWithBackquotes`: a boolean, defaults to `false`. Whether column and table names will be enclosed in backquotes.
- `statementType`: a string, one of: `insert`, `insertignore`, `update`. This governs the type of query that'll be
generated. By and large you'll want this to be set to `insert`.
generated. By and large you'll want this to be set to `insert`, the default value.
- `insertBatchSize`: an integer. This allows you to reduce the number of queries by grouping them, e.g. a single
INSERT statement to insert 10 rows at once.
- `addPrimaryKey`: a boolean. This lets you choose to add a Primary Key field to your SQL statements.
INSERT statement to insert 10 rows at once. Defaults to 1.
- `addPrimaryKey`: a boolean, defaults to `false`. This lets you choose to add a Primary Key field to your SQL
statements.

#### Conditional settings

Some settings are database specific.

- Postgres will ignore the `encloseWithBackquotes` setting.
- Postgres, SQLite, Oracle and MSSQL won't accept the `insertignore` as a value for the `statementType` setting. That
only works with MySQL.


### Example API Usage
Expand Down Expand Up @@ -48,9 +57,11 @@ Post the following JSON content to the following API path:
],
"export": {
"type": "SQL",
"settings" {
"settings": {
"tableName": "myTable",
"databaseType": "MySQL"
"databaseType": "MySQL",
"encloseWithBackquotes": true,
"createTable": true
}
}
}
Expand Down
87 changes: 62 additions & 25 deletions plugins/exportTypes/SQL/SQL.class.php
Original file line number Diff line number Diff line change
Expand Up @@ -19,12 +19,15 @@ class SQL extends ExportTypePlugin {
protected $exportTypeName = "SQL";
protected $jsModules = array("SQL.js");
protected $codeMirrorModes = array("sql");
protected $contentTypeHeader = "application/octet-stream";

public $L = array();

// stores various info about the current generation set
private $genEnvironment; // "API" or "POST"
private $template;
private $numericFields;
private $postData;
private $userSettings;
private $exportTarget;
private $isFirstBatch;
private $isLastBatch;
Expand All @@ -39,23 +42,22 @@ class SQL extends ExportTypePlugin {


function generate($generator) {
$this->genEnvironment = $generator->genEnvironment;
$this->template = $generator->getTemplateByDisplayOrder();
$this->postData = $generator->getUserSettings();
$this->userSettings = $generator->getUserSettings();
$this->exportTarget = $generator->getExportTarget();

if ($this->genEnvironment == GEN_ENVIRONMENT_API) {
$this->exportTarget = "promptDownload";
}

$this->isFirstBatch = $generator->isFirstBatch();
$this->isLastBatch = $generator->isLastBatch();
$this->data = $generator->generateExportData();
$this->currentBatchFirstRow = $generator->getCurrentBatchFirstRow();

// grab whatever settings where chosen by the user
$this->includeDropTable = isset($this->postData["etSQL_dropTable"]);
$this->createTable = isset($this->postData["etSQL_createTable"]);
$this->databaseType = $this->postData["etSQL_databaseType"];
$this->tableName = $this->postData["etSQL_tableName"];
$this->backquote = isset($this->postData["etSQL_encloseWithBackquotes"]) ? "`" : "";
$this->sqlStatementType = isset($this->postData["etSQL_statementType"]) ? $this->postData["etSQL_statementType"] : "insert";
$this->primaryKey = (isset($this->postData["etSQL_primaryKey"])) ? $this->postData["etSQL_primaryKey"] : "default";
$this->insertBatchSize = ($this->isBatchSizeValid($this->postData["etSQL_insertBatchSize"])) ? $this->postData["etSQL_insertBatchSize"] : 1;
$this->extractSettings();

foreach ($this->template as $item) {
$this->numericFields[] = isset($item["columnMetadata"]["type"]) && $item["columnMetadata"]["type"] == "numeric";
Expand Down Expand Up @@ -212,7 +214,6 @@ private function generateCreateTableSQL_MySQL() {
$endLineChar = ($this->exportTarget == "newTab") ? "<br />\n" : "\n";
$prefix = ($this->exportTarget == "newTab") ? "&nbsp;&nbsp;" : " ";


if ($this->isFirstBatch) {
if ($this->includeDropTable) {
$dropTableEndLine = ($this->exportTarget == "newTab") ? "<br /><br /><hr size=\"1\" />\n" : "\n\n";
Expand Down Expand Up @@ -457,10 +458,10 @@ private function generateCreateTableSQL_Oracle() {
}
}
$rowDataStr[] = implode(",", $displayVals);
if (count($rowDataStr) == $this->insertBatchSize) {
$content .= "INSERT INTO {$this->backquote}{$this->tableName}{$this->backquote} ($colNamesStr) VALUES (" . implode('),(', $rowDataStr) . ");$endLineChar";
$rowDataStr = array();
}
if (count($rowDataStr) == $this->insertBatchSize) {
$content .= "INSERT INTO {$this->backquote}{$this->tableName}{$this->backquote} ($colNamesStr) VALUES (" . implode('),(', $rowDataStr) . ");$endLineChar";
$rowDataStr = array();
}
} else {
$pairs = array();
for ($j=0; $j<$numCols; $j++) {
Expand All @@ -478,9 +479,9 @@ private function generateCreateTableSQL_Oracle() {
$content .= "UPDATE {$this->backquote}{$this->tableName}{$this->backquote} SET $pairsStr WHERE {$this->backquote}id{$this->backquote} = $rowNum;$endLineChar";
}
}
if (!empty($rowDataStr) && $this->sqlStatementType == "insert") {
$content .= "INSERT INTO {$this->backquote}{$this->tableName}{$this->backquote} ($colNamesStr) VALUES (" . implode('),(', $rowDataStr) . ");$endLineChar";
}
if (!empty($rowDataStr) && $this->sqlStatementType == "insert") {
$content .= "INSERT INTO {$this->backquote}{$this->tableName}{$this->backquote} ($colNamesStr) VALUES (" . implode('),(', $rowDataStr) . ");$endLineChar";
}

return $content;
}
Expand Down Expand Up @@ -567,9 +568,9 @@ private function generateCreateTableSQL_SQLite() {
$content .= "UPDATE {$this->backquote}{$this->tableName}{$this->backquote} SET $pairsStr WHERE {$this->backquote}id{$this->backquote} = $rowNum;$endLineChar";
}
}
if (!empty($rowDataStr) && $this->sqlStatementType == "insert") {
$content .= "INSERT INTO {$this->backquote}{$this->tableName}{$this->backquote} ($colNamesStr) VALUES (" . implode('),(', $rowDataStr) . ");$endLineChar";
}
if (!empty($rowDataStr) && $this->sqlStatementType == "insert") {
$content .= "INSERT INTO {$this->backquote}{$this->tableName}{$this->backquote} ($colNamesStr) VALUES (" . implode('),(', $rowDataStr) . ");$endLineChar";
}

return $content;
}
Expand Down Expand Up @@ -668,15 +669,15 @@ private function generateCreateTableSQL_MSSQL() {
}
}
}
if (!empty($rowDataStr) && $this->sqlStatementType == "insert") {
$content .= "INSERT INTO {$this->tableName}($colNamesStr) VALUES(" . implode('),(', $rowDataStr) . ");$endLineChar";
}
if (!empty($rowDataStr) && $this->sqlStatementType == "insert") {
$content .= "INSERT INTO {$this->tableName}($colNamesStr) VALUES(" . implode('),(', $rowDataStr) . ");$endLineChar";
}

return $content;
}


function wrapGeneratedContent($generatedContent) {
private function wrapGeneratedContent($generatedContent) {
$html =<<< END
<!DOCTYPE html>
<html>
Expand All @@ -693,4 +694,40 @@ function wrapGeneratedContent($generatedContent) {
END;
return $html;
}


private function extractSettings() {
if ($this->genEnvironment == GEN_ENVIRONMENT_API) {
$settings = $this->userSettings->export->settings;

// these two are enforced as required by the schema file
$this->databaseType = $settings->databaseType;
$this->tableName = $settings->tableName;

// optional values. Note the default values for each
$this->includeDropTable = property_exists($settings, "dropTable") ? $settings->dropTable : false;
$this->createTable = property_exists($settings, "createTable") ? $settings->createTable : true;
$backquote = property_exists($settings, "encloseWithBackquotes") ? $settings->encloseWithBackquotes : false;
$this->backquote = ($backquote) ? "`" : "";
$this->sqlStatementType = property_exists($settings, "statementType") ? $settings->statementType : "insert";

$addPrimaryKey = property_exists($settings, "addPrimaryKey") ? $settings->addPrimaryKey : null;
if ($addPrimaryKey == null) {
$this->primaryKey = false;
} else {
$this->primaryKey = ($addPrimaryKey) ? "default" : "none";
}

$this->insertBatchSize = property_exists($settings, "insertBatchSize") ? $settings->insertBatchSize : 1;
} else {
$this->includeDropTable = isset($this->userSettings["etSQL_dropTable"]);
$this->createTable = isset($this->userSettings["etSQL_createTable"]);
$this->databaseType = $this->userSettings["etSQL_databaseType"];
$this->tableName = $this->userSettings["etSQL_tableName"];
$this->backquote = isset($this->userSettings["etSQL_encloseWithBackquotes"]) ? "`" : "";
$this->sqlStatementType = isset($this->userSettings["etSQL_statementType"]) ? $this->userSettings["etSQL_statementType"] : "insert";
$this->primaryKey = (isset($this->userSettings["etSQL_primaryKey"])) ? $this->userSettings["etSQL_primaryKey"] : "default";
$this->insertBatchSize = ($this->isBatchSizeValid($this->userSettings["etSQL_insertBatchSize"])) ? $this->userSettings["etSQL_insertBatchSize"] : 1;
}
}
}

0 comments on commit 417104e

Please sign in to comment.