1 19 20 package org.netbeans.modules.db.sql.editor; 21 22 import java.io.BufferedWriter ; 23 import java.io.IOException ; 24 import java.io.OutputStream ; 25 import java.io.OutputStreamWriter ; 26 import java.sql.SQLException ; 27 import java.text.MessageFormat ; 28 import org.netbeans.api.db.explorer.DatabaseConnection; 29 import org.netbeans.modules.db.api.sql.execute.SQLExecuteCookie; 30 import org.netbeans.modules.db.spi.sql.editor.SQLEditorProvider; 31 import org.netbeans.modules.db.sql.editor.ui.actions.ConnectionAction; 32 import org.openide.ErrorManager; 33 import org.openide.cookies.OpenCookie; 34 import org.openide.filesystems.FileLock; 35 import org.openide.filesystems.FileObject; 36 import org.openide.filesystems.FileUtil; 37 import org.openide.filesystems.Repository; 38 import org.openide.loaders.DataObject; 39 import org.openide.loaders.DataObjectNotFoundException; 40 import org.openide.util.NbBundle; 41 42 46 public class SQLEditorProviderImpl implements SQLEditorProvider { 47 48 51 private static final String CMD_FOLDER = "Databases/SQLCommands"; 53 public void openSQLEditor(DatabaseConnection dbconn, String sql, boolean execute) { 54 FileObject root = Repository.getDefault().getDefaultFileSystem().getRoot(); 55 FileObject tmpFo = root.getFileObject(CMD_FOLDER); 56 if (tmpFo == null) { 57 try { 58 tmpFo = FileUtil.createFolder(root, CMD_FOLDER ); 59 } catch (IOException e) { 60 ErrorManager.getDefault().notify(e); 61 } 62 } 63 64 FileObject sqlFo = null; 65 66 int i = 1; 67 for (;;) { 68 String nameFmt = NbBundle.getMessage(SQLEditorProviderImpl.class, "LBL_SQLCommandFileName"); 69 String name = MessageFormat.format(nameFmt, new Object [] { new Integer (i) }); 70 try { 71 sqlFo = tmpFo.createData(name); 72 } catch (IOException e) { 73 i++; 74 continue; 75 } 76 break; 77 } 78 79 try { 80 FileLock lock = sqlFo.lock(); 81 try { 82 OutputStream stream = sqlFo.getOutputStream(lock); 83 try { 84 stream.write(0xef); 86 stream.write(0xbb); 87 stream.write(0xbf); 88 BufferedWriter writer = new BufferedWriter (new OutputStreamWriter (stream, "utf-8")); writer.write(sql); 90 writer.close(); 91 } finally { 92 stream.close(); 93 } 94 } finally { 95 lock.releaseLock(); 96 } 97 } catch (IOException e) { 98 ErrorManager.getDefault().notify(e); 99 } 100 101 DataObject sqlDo; 102 try { 103 sqlDo = DataObject.find(sqlFo); 104 } catch (DataObjectNotFoundException e) { 105 ErrorManager.getDefault().notify(e); 106 return; 107 } 108 109 OpenCookie openCookie = (OpenCookie)sqlDo.getCookie(OpenCookie.class); 110 openCookie.open(); 111 112 SQLExecuteCookie sqlCookie = (SQLExecuteCookie)sqlDo.getCookie(SQLExecuteCookie.class); 113 sqlCookie.setDatabaseConnection(dbconn); 114 if (execute) { 115 sqlCookie.execute(); 116 } 117 } 118 } 119 | Popular Tags |