PageRenderTime 41ms CodeModel.GetById 16ms RepoModel.GetById 0ms app.codeStats 0ms

/include/ImportFromSQL.h

http://sqlyog.googlecode.com/
C Header | 342 lines | 79 code | 65 blank | 198 comment | 0 complexity | b3f8f8e288166dd591b2eefdc3b877f9 MD5 | raw file
Possible License(s): GPL-2.0
  1. /* Copyright (C) 2013 Webyog Inc.
  2. This program is free software; you can redistribute it and/or modify
  3. it under the terms of the GNU General Public License as published by
  4. the Free Software Foundation; either version 2 of the License, or
  5. (at your option) any later version.
  6. This program is distributed in the hope that it will be useful,
  7. but WITHOUT ANY WARRANTY; without even the implied warranty of
  8. MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
  9. GNU General Public License for more details.
  10. You should have received a copy of the GNU General Public License
  11. along with this program; if not, write to the Free Software
  12. Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307, USA
  13. */
  14. #include "MDIWindow.h"
  15. #include "Tunnel.h"
  16. #include "wyString.h"
  17. #ifndef __YOGEXPORT__
  18. #define __YOGEXPORT__
  19. /**
  20. * Wrapper for all the import return values.
  21. */
  22. typedef enum __import_errors
  23. {
  24. SYSTEMERROR=0,/**< SYSTEMERROR, unexpected system failure */
  25. GUIROUTINEERROR,/**< GUIROUTINEERROR, GUI routine error */
  26. MYSQLERROR,/**< MYSQLERROR, MySQL error */
  27. FILEERROR,/**< FILEERROR, File access error */
  28. IMPORTSTOPPED,/**< IMPORTSTOPPED, Importing is stopped */
  29. SUCCESS,/**< SUCCESS, Importing successful */
  30. SUCCESSWITHERROR/**<SUCCESS,Importing successful when abort on error is not checked*/
  31. } IMPORTERROR;
  32. /**
  33. * A macro that returns maximum object name length.
  34. */
  35. #define MAX_OBJECT_NAME 128
  36. /**
  37. * A macro that returns maximum filename.
  38. */
  39. #define MAX_FILE_NAME 1024
  40. /**
  41. * A macro that returns maximum query length.
  42. */
  43. #define MAX_QUERY_LEN 1024
  44. #define ER_EMPTY_QUERY 1065
  45. /* callback function to display any GUI related stuff.
  46. this is always called when we are reading a file */
  47. typedef void (*gui_update_routin)(
  48. void * lpParam, long long int num_bytes, long long int num_queries
  49. );
  50. typedef gui_update_routin LPGUI_UPDATE_ROUTINE;
  51. /*! \class ImportFromSQL
  52. \brief Helps to import the sql statements from a file.
  53. This class imports batch files in to MySQL database, Exported bye CYogExport.
  54. In this process it connects to MySQL server opens batch file ,
  55. and it will read 1024 bites at a time in to a buffer ,
  56. from that it will take a single line of query to issue mysql query.
  57. It will find out single query with the help of delimiter " ; ",
  58. it reads data in to query buffer until it reaches the delimiter ,
  59. and at the same time it calculates the length of the string,
  60. and while parsing it will check for the comments and it will
  61. skip those comments from execution. To optimize stuff always the
  62. file is read into the same buffer to reduce the number of allocation/dellocations.
  63. */
  64. class ImportFromSQL
  65. {
  66. public:
  67. /// Default constructor.
  68. /**
  69. * Initializes the member variables with the default values.
  70. */
  71. ImportFromSQL();
  72. /// Destructor
  73. /**
  74. Free up all the allocated resources
  75. */
  76. ~ImportFromSQL();
  77. /// Initializes the tunnelmode
  78. /** Sets whether the tunnelmode is using or not.
  79. @param val : IN wyTrue/wyFalse.
  80. @returns void.
  81. */
  82. void SetTunnel(wyBool val);
  83. /// Sets the error filename
  84. /**
  85. @param errorfile : IN Error file name.
  86. @returns void.
  87. */
  88. void SetErrorFile(wyString &errorfile);
  89. /// Sets the import filename
  90. /**
  91. @param importfile : IN Import file name.
  92. @returns void.
  93. */
  94. void SetImportFile(wyString &importfile);
  95. /// Sets the tunnel host site
  96. /**
  97. @param hostsite : IN host site url.
  98. @returns void.
  99. */
  100. void SetTunnelHostSite(wyString hostsite);
  101. /// Sets host information
  102. /**
  103. @param host : IN host address.
  104. @param user : IN user name.
  105. @param pass : IN password.
  106. @param dbname : IN host database name.
  107. @param port : IN host port.
  108. @returns void.
  109. */
  110. void SetHostInfo(wyString host, wyString user, wyString pass, wyString dbname, int port);
  111. /// Sets the Content-Type
  112. /**
  113. @param contenttype : IN contenttype.
  114. @returns void.
  115. */
  116. void SetContentType(wyString contenttype);
  117. //Set the encoding style BASE64 or not
  118. /**
  119. @param isbase64encode : IN it's wyTrue for Base64 encode needed
  120. @return void
  121. */
  122. void SetImportEncodingScheme(bool isbase64encode);
  123. //Set the Abort on error flag
  124. /***
  125. @param abourt_on_error : If the checkbox for abort on nerror is set or not
  126. @return void
  127. */
  128. void SetAbortFlag(wyBool abort_on_error);
  129. /// This function will do the import according to the values set as well as the parameter given.
  130. /**
  131. @param num_bytes : OUT number of bytes read.
  132. @param num_queries : OUT number of queries executed.
  133. @param gui_routine : OUT callback function to display the info about importing.
  134. @param lpParam : IN/OUT IMPORTBATCH structure containing all required values.
  135. @param stopimport : IN/OUT Flag that is used to handle the thread.
  136. @param wnd : IN con. window pointer
  137. @returns IMPORTERROR showing the status.
  138. @sa typedef enum __import_errors
  139. @sa typedef struct __stimportbatch
  140. */
  141. IMPORTERROR Import(wyUInt32 *num_bytes, wyUInt32 *num_queries,
  142. LPGUI_UPDATE_ROUTINE gui_routine, void * lpParam, wyBool * stopimport, MDIWindow *wnd);
  143. private:
  144. ///variable for Abort on error
  145. wyBool m_abort_on_error;
  146. ///variable to check non of the executed queries had error.This is useful when u have to ignore abort on error.
  147. wyBool m_errorfree;
  148. /// Mysql port
  149. wyInt32 m_port;
  150. /// MySQl error number
  151. wyInt32 m_errno;
  152. /// pointer to keep the IMPORTBATCH
  153. void *m_lp_param;
  154. /// DataBase name
  155. wyString m_db;
  156. /// Import file name
  157. wyString m_file;
  158. /// Host address
  159. wyString m_host;
  160. /// user name
  161. wyString m_user;
  162. /// Password
  163. wyString m_pass;
  164. /// Error file name
  165. wyString m_errfile;
  166. /// pointer to Tunnel host site url
  167. wyString m_tunnel_host_site;
  168. /// Status of tunnel
  169. wyBool m_mode_of_tunnel;
  170. ///Content-type
  171. wyString m_contenttype;
  172. //Whether the Base64 encoding needed or not
  173. wyBool m_isbase64encode;
  174. /// Error description pointer
  175. const wyChar *m_error;
  176. /// Number of queries executed
  177. wyUInt32 m_numquery;
  178. /// Total number of bytes to read
  179. wyUInt32 m_numbytes;
  180. /// Number of bytes read
  181. wyUInt32 m_bytesread;
  182. /// Line number
  183. wyUInt32 m_line_number;
  184. /// File handle
  185. FILE *m_importfile;
  186. /// mysql pointer
  187. MYSQL *m_mysql;
  188. /// tunnel pointer
  189. Tunnel *m_tunnel;
  190. /// GUI call back routine.
  191. LPGUI_UPDATE_ROUTINE m_gui_routine;
  192. /// initialize the import with default values
  193. /**
  194. @return void
  195. */
  196. void Initialize();
  197. /// Releases the MySQl resources
  198. /**
  199. @return wyBool, wyTrue if success, otherwise wyFalse.
  200. */
  201. wyBool SafeExit();
  202. /// Writes the error message in SQLyog.err
  203. /**
  204. @param errmsg : IN The query/errormsg that returned error, which is also logged in error file
  205. @return wyBool, wyTrue if success, otherwise wyFalse.
  206. */
  207. wyBool OnError(const wyChar * errmssg);
  208. /// Connects to the mysql server
  209. /**
  210. @return wyBool, wyTrue if success, otherwise wyFalse.
  211. */
  212. wyBool MySQLConnect();
  213. /// Opens the import file for reading
  214. /**
  215. @return wyBool, wyTrue if success, otherwise wyFalse.
  216. */
  217. wyBool OpenImportFile();
  218. /// Initiates the import process
  219. /**
  220. @param stopimport : IN flag to control the import process
  221. @param wnd : IN con. window pointer
  222. @return wyBool, wyTrue if success, otherwise wyFalse.
  223. */
  224. IMPORTERROR StartImport(wyBool * stopimport, MDIWindow *wnd);
  225. /// Starts the import process
  226. /**
  227. @param stopimport : IN flag to control the import process
  228. @param isfkchkhttp : IN flag tells whether set FK check = 0 if its wyTrue.
  229. @return wyBool, wyTrue if success, otherwise wyFalse.
  230. */
  231. wyBool DoImport(wyBool * stopimport, wyBool isfkchkhttp);
  232. /// Executes a MySQL query on the connected server
  233. /**
  234. @param query : IN Query to be executed
  235. @param m_str_length : IN Query length
  236. @return wyBool, wyTrue if success, otherwise wyFalse.
  237. */
  238. wyBool ExecuteQuery(wyString& query, wyUInt32 m_str_length, bool isread, wyBool isfkchkhttp);
  239. /// Changes the server context database
  240. /**
  241. @param query : IN Query to be executed
  242. @return wyBool, wyTrue if success, otherwise wyFalse.
  243. */
  244. wyBool ChangeContextDB(wyString& query);
  245. /// Gets the database name from the USE query
  246. /**
  247. @param query : IN Query to be executed
  248. @param db : OUT database name
  249. @return void
  250. */
  251. void GetDBFromQuery(wyString& query, wyString& db);
  252. void GetSelectDB(wyString& importdb);
  253. /// Removes the spaces from the left of text
  254. /**
  255. @param text : IN text to be trimmed
  256. @return The trimmed text
  257. */
  258. const wyChar* LeftPadText(const wyChar * text);
  259. /// Checks for bad char for XML
  260. /**
  261. @param text : IN text to be checked
  262. @return wyBool, wyTrue if success, otherwise wyFalse.
  263. */
  264. wyBool IsBadforXML(const wyChar * text);
  265. /// Checks whether the buffer contains space only or not
  266. /**
  267. @param text : IN buff to be checked
  268. @return wyBool, wyTrue if success, otherwise wyFalse.
  269. */
  270. wyBool IsSpace(const wyChar * buff);
  271. };
  272. #endif