From c37b59b4dc6c15108bd269988a11bd2051ac2edf Mon Sep 17 00:00:00 2001
From: Nikola Kotur <kotnik@drupal.rs>
Date: Thu, 12 May 2011 14:25:20 +0200
Subject: [PATCH 1/3] Issue #880154: sql query file support

---
 commands/sql/sql.drush.inc |   13 +++++++++++--
 1 files changed, 11 insertions(+), 2 deletions(-)

diff --git a/commands/sql/sql.drush.inc b/commands/sql/sql.drush.inc
index 0bcdee1..eb178a0 100644
--- a/commands/sql/sql.drush.inc
+++ b/commands/sql/sql.drush.inc
@@ -80,12 +80,13 @@ function sql_drush_command() {
     'description' => 'Execute a query against the site database.',
     'examples' => array(
       'drush sql-query "SELECT * FROM {users} WHERE uid=1"' => 'Browse user record. Table prefixes are honored.',
-      '`drush sql-connect` < example.sql' => 'Import sql statements from a file into the current database.',
+      'drush sql-query --file example.sql' => 'Import sql statements from a file into the current database.',
     ),
     'arguments' => array(
        'query' => 'An SQL query.',
     ),
     'options' => array(
+      'file' => 'File with SQL statements.',
       'extra' => 'Add custom options to the mysql command.',
     ) + $options,
     'aliases' => array('sqlq'),
@@ -437,13 +438,21 @@ function _drush_sql_get_table_list($option_name) {
 /**
  * Command callback. Executes the given SQL query on the Drupal database.
  */
-function drush_sql_query($query) {
+function drush_sql_query($query='') {
   return _drush_sql_query($query);
 }
 
 function _drush_sql_query($query, $db_spec = NULL) {
   $scheme = _drush_sql_get_scheme($db_spec);
 
+  if (!$query) {
+    $input_file = drush_get_option('file');
+    if (!$input_file) {
+      return drush_set_error('DRUSH_SQL_NO_QUERY', dt('Either provide query or file with SQL statements.'));
+    }
+    $query = file_get_contents($input_file);
+  }
+
   // Inject table prefixes as needed.
   if (drush_has_boostrapped(DRUSH_BOOTSTRAP_DRUPAL_DATABASE)) {
     if (drush_drupal_major_version() >= 7) {
-- 
1.7.4.1


From 06bbc83368592aaa34612ed12b69d40122bed109 Mon Sep 17 00:00:00 2001
From: Nikola Kotur <kotnik@drupal.rs>
Date: Thu, 12 May 2011 14:29:48 +0200
Subject: [PATCH 2/3] Issue #880154: Documentation fix

---
 commands/sql/sql.drush.inc |    2 +-
 1 files changed, 1 insertions(+), 1 deletions(-)

diff --git a/commands/sql/sql.drush.inc b/commands/sql/sql.drush.inc
index eb178a0..fdecc4a 100644
--- a/commands/sql/sql.drush.inc
+++ b/commands/sql/sql.drush.inc
@@ -80,7 +80,7 @@ function sql_drush_command() {
     'description' => 'Execute a query against the site database.',
     'examples' => array(
       'drush sql-query "SELECT * FROM {users} WHERE uid=1"' => 'Browse user record. Table prefixes are honored.',
-      'drush sql-query --file example.sql' => 'Import sql statements from a file into the current database.',
+      'drush sql-query --file=example.sql' => 'Import SQL statements from a file into the current database.',
     ),
     'arguments' => array(
        'query' => 'An SQL query.',
-- 
1.7.4.1


From 0dd29e0648025b5ccab372c34fdfe76a916244d9 Mon Sep 17 00:00:00 2001
From: Nikola Kotur <kotnik@drupal.rs>
Date: Fri, 20 May 2011 16:38:07 +0200
Subject: [PATCH 3/3] Issue #880154: Moved file check to drush_sql_query

---
 commands/sql/sql.drush.inc |   12 ++++++------
 1 files changed, 6 insertions(+), 6 deletions(-)

diff --git a/commands/sql/sql.drush.inc b/commands/sql/sql.drush.inc
index fdecc4a..6cc182e 100644
--- a/commands/sql/sql.drush.inc
+++ b/commands/sql/sql.drush.inc
@@ -439,12 +439,6 @@ function _drush_sql_get_table_list($option_name) {
  * Command callback. Executes the given SQL query on the Drupal database.
  */
 function drush_sql_query($query='') {
-  return _drush_sql_query($query);
-}
-
-function _drush_sql_query($query, $db_spec = NULL) {
-  $scheme = _drush_sql_get_scheme($db_spec);
-
   if (!$query) {
     $input_file = drush_get_option('file');
     if (!$input_file) {
@@ -453,6 +447,12 @@ function _drush_sql_query($query, $db_spec = NULL) {
     $query = file_get_contents($input_file);
   }
 
+  return _drush_sql_query($query);
+}
+
+function _drush_sql_query($query, $db_spec = NULL) {
+  $scheme = _drush_sql_get_scheme($db_spec);
+
   // Inject table prefixes as needed.
   if (drush_has_boostrapped(DRUSH_BOOTSTRAP_DRUPAL_DATABASE)) {
     if (drush_drupal_major_version() >= 7) {
-- 
1.7.4.1

