1
0

easy-mysql.inc 84 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192939495969798991001011021031041051061071081091101111121131141151161171181191201211221231241251261271281291301311321331341351361371381391401411421431441451461471481491501511521531541551561571581591601611621631641651661671681691701711721731741751761771781791801811821831841851861871881891901911921931941951961971981992002012022032042052062072082092102112122132142152162172182192202212222232242252262272282292302312322332342352362372382392402412422432442452462472482492502512522532542552562572582592602612622632642652662672682692702712722732742752762772782792802812822832842852862872882892902912922932942952962972982993003013023033043053063073083093103113123133143153163173183193203213223233243253263273283293303313323333343353363373383393403413423433443453463473483493503513523533543553563573583593603613623633643653663673683693703713723733743753763773783793803813823833843853863873883893903913923933943953963973983994004014024034044054064074084094104114124134144154164174184194204214224234244254264274284294304314324334344354364374384394404414424434444454464474484494504514524534544554564574584594604614624634644654664674684694704714724734744754764774784794804814824834844854864874884894904914924934944954964974984995005015025035045055065075085095105115125135145155165175185195205215225235245255265275285295305315325335345355365375385395405415425435445455465475485495505515525535545555565575585595605615625635645655665675685695705715725735745755765775785795805815825835845855865875885895905915925935945955965975985996006016026036046056066076086096106116126136146156166176186196206216226236246256266276286296306316326336346356366376386396406416426436446456466476486496506516526536546556566576586596606616626636646656666676686696706716726736746756766776786796806816826836846856866876886896906916926936946956966976986997007017027037047057067077087097107117127137147157167177187197207217227237247257267277287297307317327337347357367377387397407417427437447457467477487497507517527537547557567577587597607617627637647657667677687697707717727737747757767777787797807817827837847857867877887897907917927937947957967977987998008018028038048058068078088098108118128138148158168178188198208218228238248258268278288298308318328338348358368378388398408418428438448458468478488498508518528538548558568578588598608618628638648658668678688698708718728738748758768778788798808818828838848858868878888898908918928938948958968978988999009019029039049059069079089099109119129139149159169179189199209219229239249259269279289299309319329339349359369379389399409419429439449459469479489499509519529539549559569579589599609619629639649659669679689699709719729739749759769779789799809819829839849859869879889899909919929939949959969979989991000100110021003100410051006100710081009101010111012101310141015101610171018101910201021102210231024102510261027102810291030103110321033103410351036103710381039104010411042104310441045104610471048104910501051105210531054105510561057105810591060106110621063106410651066106710681069107010711072107310741075107610771078107910801081108210831084108510861087108810891090109110921093109410951096109710981099110011011102110311041105110611071108110911101111111211131114111511161117111811191120112111221123112411251126112711281129113011311132113311341135113611371138113911401141114211431144114511461147114811491150115111521153115411551156115711581159116011611162116311641165116611671168116911701171117211731174117511761177117811791180118111821183118411851186118711881189119011911192119311941195119611971198119912001201120212031204120512061207120812091210121112121213121412151216121712181219122012211222122312241225122612271228122912301231123212331234123512361237123812391240124112421243124412451246124712481249125012511252125312541255125612571258125912601261126212631264126512661267126812691270127112721273127412751276127712781279128012811282128312841285128612871288128912901291129212931294129512961297129812991300130113021303130413051306130713081309131013111312131313141315131613171318131913201321132213231324132513261327132813291330133113321333133413351336133713381339134013411342134313441345134613471348134913501351135213531354135513561357135813591360136113621363136413651366136713681369137013711372137313741375137613771378137913801381138213831384138513861387138813891390139113921393139413951396139713981399140014011402140314041405140614071408140914101411141214131414141514161417141814191420142114221423142414251426142714281429143014311432143314341435143614371438143914401441144214431444144514461447144814491450145114521453145414551456145714581459146014611462146314641465146614671468146914701471147214731474147514761477147814791480148114821483148414851486148714881489149014911492149314941495149614971498149915001501150215031504150515061507150815091510151115121513151415151516151715181519152015211522152315241525152615271528152915301531153215331534153515361537153815391540154115421543154415451546154715481549155015511552155315541555155615571558155915601561156215631564156515661567156815691570157115721573157415751576157715781579158015811582158315841585158615871588158915901591159215931594159515961597159815991600160116021603160416051606160716081609161016111612161316141615161616171618161916201621162216231624162516261627162816291630163116321633163416351636163716381639164016411642164316441645164616471648164916501651165216531654165516561657165816591660166116621663166416651666166716681669167016711672167316741675167616771678167916801681168216831684168516861687168816891690169116921693169416951696169716981699170017011702170317041705170617071708170917101711171217131714171517161717171817191720172117221723172417251726172717281729173017311732173317341735173617371738173917401741174217431744174517461747174817491750175117521753175417551756175717581759176017611762176317641765176617671768176917701771177217731774177517761777177817791780178117821783178417851786178717881789179017911792179317941795179617971798179918001801180218031804180518061807180818091810181118121813181418151816181718181819182018211822182318241825182618271828182918301831183218331834183518361837183818391840184118421843184418451846184718481849185018511852185318541855185618571858185918601861186218631864186518661867186818691870187118721873187418751876187718781879188018811882188318841885188618871888188918901891189218931894189518961897189818991900190119021903190419051906190719081909191019111912191319141915191619171918191919201921192219231924192519261927192819291930193119321933193419351936193719381939194019411942194319441945194619471948194919501951195219531954195519561957195819591960196119621963196419651966196719681969197019711972197319741975197619771978197919801981198219831984198519861987198819891990199119921993199419951996199719981999200020012002200320042005200620072008200920102011201220132014201520162017201820192020202120222023202420252026202720282029203020312032203320342035203620372038203920402041204220432044204520462047204820492050205120522053205420552056205720582059206020612062206320642065206620672068206920702071207220732074207520762077207820792080208120822083208420852086208720882089209020912092209320942095209620972098209921002101210221032104210521062107210821092110211121122113211421152116211721182119212021212122212321242125212621272128212921302131213221332134213521362137213821392140214121422143214421452146214721482149215021512152215321542155215621572158215921602161216221632164216521662167216821692170217121722173217421752176217721782179218021812182218321842185218621872188218921902191219221932194219521962197219821992200220122022203220422052206220722082209221022112212221322142215221622172218221922202221222222232224222522262227222822292230223122322233223422352236223722382239224022412242224322442245224622472248224922502251225222532254225522562257225822592260226122622263226422652266226722682269227022712272227322742275227622772278227922802281228222832284228522862287228822892290229122922293229422952296229722982299230023012302230323042305230623072308230923102311231223132314231523162317231823192320232123222323232423252326232723282329233023312332233323342335233623372338233923402341234223432344234523462347234823492350235123522353235423552356235723582359236023612362236323642365236623672368236923702371237223732374237523762377237823792380238123822383238423852386238723882389239023912392239323942395239623972398239924002401240224032404240524062407240824092410241124122413241424152416241724182419242024212422242324242425242624272428242924302431243224332434243524362437243824392440244124422443244424452446244724482449245024512452245324542455245624572458245924602461246224632464246524662467246824692470247124722473247424752476247724782479248024812482248324842485248624872488248924902491249224932494249524962497249824992500250125022503250425052506250725082509251025112512251325142515251625172518251925202521252225232524252525262527252825292530
  1. /*
  2. _ ____ _____
  3. | | |___ \ | ____|
  4. ___ __ _ ___ _ _ ______ _ __ ___ _ _ ___ __ _ | | __ __ __) | | |__
  5. / _ \ / _` | / __| | | | | |______| | '_ ` _ \ | | | | / __| / _` | | | \ \ / / |__ < |___
  6. | __/ | (_| | \__ \ | |_| | | | | | | | | |_| | \__ \ | (_| | | | \ V / ___) | _ ___) |
  7. \___| \__,_| |___/ \__, | |_| |_| |_| \__, | |___/ \__, | |_| \_/ |____/ (_) |____/
  8. __/ | __/ | | |
  9. |___/ |___/ |_|
  10. Portions of this code are Copyright (C) 2015 ThreeKingz the Original Author
  11. Licensed under the Apache License, Version 2.0 (the "License");
  12. you may not use this file except in compliance with the License.
  13. You may obtain a copy of the License at
  14. http://www.apache.org/licenses/LICENSE-2.0
  15. Unless required by applicable law or agreed to in writing, software
  16. distributed under the License is distributed on an "AS IS" BASIS,
  17. WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
  18. See the License for the specific language governing permissions and
  19. limitations under the License.
  20. Credits:
  21. Original Author: (creator)
  22. * Freddy Borja - ThePhenix AKA ThreeKingz
  23. Author: (current developer)
  24. * eco1999 AKA Max_Andolini
  25. Version: 3.6
  26. SQL_ReadRetrievedRows has been changed and now can be used as SQL::ReadRetrievedRows to match the existing style.
  27. */
  28. #if defined _tksql_included//Avoid including it again!
  29. #endinput
  30. #endif
  31. #define _tksql_included
  32. #if !defined _samp_included
  33. #tryinclude <a_samp>
  34. #if !defined _samp_included
  35. #error "<a_samp.inc> was not found. Make sure the include is correctly placed on your includes folder."
  36. #endif
  37. #endif
  38. #if !defined mysql_included
  39. #tryinclude <a_mysql>
  40. #if !defined mysql_included
  41. #error "<a_mysql> was not found on your includes folder. Make sure you have the a_mysql include by BlueG on your includes folder."
  42. #endif
  43. #endif
  44. //By Y_Less
  45. //native strcpy(dest[], src[], len = sizeof(dest));
  46. #if !defined strcpy
  47. #define strcpy(%0,%1) \
  48. strcat((%0[0] = '\0', %0), %1)
  49. #endif
  50. #if !defined isnull
  51. #define isnull(%1) \
  52. ((!(%1[0])) || (((%1[0]) == '\1') && (!(%1[1]))))
  53. #endif
  54. #if !defined SQL_MAX_HANDLES
  55. #define SQL_MAX_HANDLES (3)//Maximun concurrent handles running.
  56. #endif
  57. #define SQL_MAX_TABLE_NAME (64)
  58. #define SQL_MAX_QUERY_LENGTH (8192)
  59. #define SQL_MAX_INDEXES (4)
  60. #define SQL_INVALID_HANDLE (-1)
  61. #define SQL_FORM_LENGTH (256)
  62. #define SQL:: SQL_T
  63. #define SQL_Warning(%0) (printf("[MYSQL] - WARNING: " %0))
  64. #define SQL_Error(%0) (printf("[MYSQL] - ERROR: " %0))
  65. #define SQL_Notice(%0) (printf("[MYSQL] - NOTICE: " %0))
  66. /*==============================================================================
  67. Internal variables and functions
  68. ================================================================================*/
  69. enum SQL::datatypes {SQL_TYPE_INT, SQL_TYPE_VCHAR, SQL_TYPE_FLOAT}
  70. enum SQL::qtypes {SQL::UPDATE, SQL::UPDATE2, SQL::TUPDATE, SQL::CREATE, SQL::INSERT, SQL_TYPE_DELETE, SQL::READ, SQL::READ2, SQL::TREAD, SQL::CALLBACK, SQL::MREAD, SQL::MREAD2, SQL::MTREAD}
  71. enum SQL::ftypes {SQL::CASCADE, SQL::SETNULL, SQL::NOACTION, SQL::RESTRICT}
  72. static stock
  73. bool:SQL::UsedHandle[SQL_MAX_HANDLES],
  74. MySQL:SQL::upd_connectionHandle[SQL_MAX_HANDLES],
  75. SQL::upd_table[SQL_MAX_HANDLES][SQL_MAX_TABLE_NAME],
  76. SQL::upd_rowidentifier[SQL_MAX_HANDLES][SQL_FORM_LENGTH],
  77. SQL::upd_query[SQL_MAX_HANDLES][SQL_MAX_QUERY_LENGTH],
  78. SQL::upd_query_2[SQL_MAX_HANDLES][SQL_MAX_QUERY_LENGTH],
  79. SQL::upd_increment_key[SQL_MAX_HANDLES][SQL_FORM_LENGTH],
  80. SQL::upd_form[SQL_FORM_LENGTH],
  81. SQL::upd_form2[SQL_FORM_LENGTH],
  82. SQL::upd_datacount[SQL_MAX_HANDLES],
  83. SQL::qtypes:SQL::upd_type[SQL_MAX_HANDLES],
  84. SQL::upd_pos[SQL_MAX_HANDLES],
  85. Cache:SQL::ReadCache[SQL_MAX_HANDLES],
  86. bool:SQL::upd_useautoincrement[SQL_MAX_HANDLES],
  87. SQL::primarykey[SQL_MAX_HANDLES][64],
  88. SQL::index_set[SQL_MAX_HANDLES][SQL_MAX_INDEXES],
  89. SQL::index[SQL_MAX_HANDLES][SQL_MAX_INDEXES][64],
  90. SQL::isset_primarykey[SQL_MAX_HANDLES]
  91. ;
  92. stock IsMultiReadHandleValid(handle)
  93. {
  94. if(!SQL::IsValidUpdatingSlot(handle)) return 0;
  95. if(SQL::upd_type[handle] != SQL::CALLBACK) return 0;
  96. return 1;
  97. }
  98. static stock SQL::GetFreeUpdatingSlot()
  99. {
  100. new i = 0;
  101. while (i < sizeof (SQL::UsedHandle) && SQL::UsedHandle[i] == true)
  102. {
  103. i++;
  104. }
  105. if(i == sizeof (SQL::UsedHandle)) return SQL_INVALID_HANDLE;
  106. return i;
  107. }
  108. static stock SQL::GetFreeIndexSlot(handle)
  109. {
  110. new i = 0;
  111. while (i < sizeof (SQL::index_set) && SQL::index_set[handle][i] == 1)
  112. {
  113. i++;
  114. }
  115. if(i == sizeof (SQL::index_set)) return SQL_INVALID_HANDLE;
  116. return i;
  117. }
  118. static stock SQL::RetFType(SQL::ftypes:type)
  119. {
  120. new SQL::fname[64];
  121. switch(type)
  122. {
  123. case SQL::CASCADE: SQL::fname = "CASCADE";
  124. case SQL::SETNULL: SQL::fname = "SET NULL";
  125. case SQL::RESTRICT: SQL::fname = "RESTRICT";
  126. case SQL::NOACTION: SQL::fname = "NO ACTION";
  127. }
  128. return SQL::fname;
  129. }
  130. static stock SQL::IsValidUpdatingSlot(handle)
  131. {
  132. return SQL::UsedHandle[handle];
  133. }
  134. static stock SQL::OpenTable_MultiRead(const table[], const column_where[] = "", row_identifier = -1, limit = -1, limit2 = -1, const desc[] = "", MySQL:connectionHandle = MYSQL_DEFAULT_HANDLE)
  135. {
  136. if(strlen(table) > SQL_MAX_TABLE_NAME)
  137. {
  138. SQL_Error("(SQL::OpenTable) Invalid table length.");
  139. return SQL_INVALID_HANDLE;
  140. }
  141. new
  142. i = SQL::GetFreeUpdatingSlot()
  143. ;
  144. if(i == SQL_INVALID_HANDLE)
  145. {
  146. return SQL_INVALID_HANDLE;
  147. }
  148. SQL::upd_type[i] = SQL::CALLBACK;
  149. format(SQL::upd_form, sizeof(SQL::upd_form), "SELECT * FROM `%s`", table);
  150. if(!isnull(column_where))
  151. {
  152. format(SQL::upd_form2, sizeof(SQL::upd_form2), " WHERE `%s`='%d'", column_where, row_identifier);
  153. strcat(SQL::upd_form, SQL::upd_form2);
  154. }
  155. if(!isnull(desc))
  156. {
  157. format(SQL::upd_form2, sizeof(SQL::upd_form2), " ORDER BY `%s` DESC", desc);
  158. strcat(SQL::upd_form, SQL::upd_form2);
  159. }
  160. if(limit != -1 && limit2 != -1)
  161. {
  162. format(SQL::upd_form2, sizeof(SQL::upd_form2), " LIMIT %d, %d", limit, limit2);
  163. strcat(SQL::upd_form, SQL::upd_form2);
  164. }
  165. if(limit != -1 && limit2 == -1)
  166. {
  167. format(SQL::upd_form2, sizeof(SQL::upd_form2), " LIMIT %d", limit);
  168. strcat(SQL::upd_form, SQL::upd_form2);
  169. }
  170. strcat(SQL::upd_form, " ");
  171. SQL::ReadCache[i] = mysql_query(connectionHandle, SQL::upd_form);
  172. if(mysql_errno() != 0)
  173. {
  174. return SQL_Warning("Query could not be completed due to error: %s", SQL::upd_form);
  175. }
  176. format(SQL::upd_query[i], SQL_MAX_QUERY_LENGTH, "%s", SQL::upd_form);
  177. SQL::upd_datacount[i] = 0;
  178. if(cache_is_valid(SQL::ReadCache[i]))
  179. {
  180. SQL::UsedHandle[i] = true;
  181. SQL::upd_connectionHandle[i] = connectionHandle;
  182. return i;
  183. }
  184. return SQL_INVALID_HANDLE;
  185. }
  186. static stock SQL::OpenTable_MultiRead2(const table[], const column_where[] = "", row_identifier = -1, const column_where2[] = "", row_identifier2 = -1, const row_identifier3[] = "", limit = -1, limit2 = -1, const desc[] = "", MySQL:connectionHandle = MYSQL_DEFAULT_HANDLE)
  187. {
  188. if(strlen(table) > SQL_MAX_TABLE_NAME)
  189. {
  190. SQL_Error("(SQL::OpenTable) Invalid table length.");
  191. return SQL_INVALID_HANDLE;
  192. }
  193. new
  194. i = SQL::GetFreeUpdatingSlot()
  195. ;
  196. if(i == SQL_INVALID_HANDLE)
  197. {
  198. return SQL_INVALID_HANDLE;
  199. }
  200. SQL::upd_type[i] = SQL::CALLBACK;
  201. format(SQL::upd_form, sizeof(SQL::upd_form), "SELECT * FROM `%s`", table);
  202. if(!isnull(column_where))
  203. {
  204. format(SQL::upd_form2, sizeof(SQL::upd_form2), " WHERE `%s`='%d'", column_where, row_identifier);
  205. strcat(SQL::upd_form, SQL::upd_form2);
  206. }
  207. if(!isnull(column_where2) && row_identifier2 != -1 && isnull(row_identifier3))
  208. {
  209. format(SQL::upd_form2, sizeof(SQL::upd_form2), " AND `%s`='%d'", column_where2, row_identifier2);
  210. strcat(SQL::upd_form, SQL::upd_form2);
  211. }
  212. if(!isnull(column_where2) && !isnull(row_identifier3) && row_identifier2 == -1)
  213. {
  214. format(SQL::upd_form2, sizeof(SQL::upd_form2), " AND `%s`='%s'", column_where2, row_identifier3);
  215. strcat(SQL::upd_form, SQL::upd_form2);
  216. }
  217. if(!isnull(desc))
  218. {
  219. format(SQL::upd_form2, sizeof(SQL::upd_form2), " ORDER BY `%s` DESC", desc);
  220. strcat(SQL::upd_form, SQL::upd_form2);
  221. }
  222. if(limit != -1 && limit2 != -1)
  223. {
  224. format(SQL::upd_form2, sizeof(SQL::upd_form2), " LIMIT %d, %d", limit, limit2);
  225. strcat(SQL::upd_form, SQL::upd_form2);
  226. }
  227. if(limit != -1 && limit2 == -1)
  228. {
  229. format(SQL::upd_form2, sizeof(SQL::upd_form2), " LIMIT %d", limit);
  230. strcat(SQL::upd_form, SQL::upd_form2);
  231. }
  232. strcat(SQL::upd_form, " ");
  233. SQL::ReadCache[i] = mysql_query(connectionHandle, SQL::upd_form);
  234. if(mysql_errno() != 0)
  235. {
  236. return SQL_Warning("Query could not be completed due to error: %s", SQL::upd_form);
  237. }
  238. format(SQL::upd_query[i], SQL_MAX_QUERY_LENGTH, "%s", SQL::upd_form);
  239. SQL::upd_datacount[i] = 0;
  240. if(cache_is_valid(SQL::ReadCache[i]))
  241. {
  242. SQL::UsedHandle[i] = true;
  243. SQL::upd_connectionHandle[i] = connectionHandle;
  244. return i;
  245. }
  246. return SQL_INVALID_HANDLE;
  247. }
  248. static stock SQL::OpenTable_MultiReadEx(const table[], const column_where[] = "", const row_identifier[] = "", limit = -1, limit2 = -1, const desc[] = "", MySQL:connectionHandle = MYSQL_DEFAULT_HANDLE)
  249. {
  250. if(strlen(table) > SQL_MAX_TABLE_NAME)
  251. {
  252. SQL_Error("(SQL::OpenTable) Invalid table length.");
  253. return SQL_INVALID_HANDLE;
  254. }
  255. new
  256. i = SQL::GetFreeUpdatingSlot()
  257. ;
  258. if(i == SQL_INVALID_HANDLE)
  259. {
  260. return SQL_INVALID_HANDLE;
  261. }
  262. SQL::upd_type[i] = SQL::CALLBACK;
  263. format(SQL::upd_form, sizeof(SQL::upd_form), "SELECT * FROM `%s`", table);
  264. if(!isnull(column_where))
  265. {
  266. format(SQL::upd_form2, sizeof(SQL::upd_form2), " WHERE `%s`='%s'", column_where, row_identifier);
  267. strcat(SQL::upd_form, SQL::upd_form2);
  268. }
  269. if(!isnull(desc))
  270. {
  271. format(SQL::upd_form2, sizeof(SQL::upd_form2), " ORDER BY `%s` DESC", desc);
  272. strcat(SQL::upd_form, SQL::upd_form2);
  273. }
  274. if(limit != -1 && limit2 != -1)
  275. {
  276. format(SQL::upd_form2, sizeof(SQL::upd_form2), " LIMIT %d, %d", limit, limit2);
  277. strcat(SQL::upd_form, SQL::upd_form2);
  278. }
  279. if(limit != -1 && limit2 == -1)
  280. {
  281. format(SQL::upd_form2, sizeof(SQL::upd_form2), " LIMIT %d", limit);
  282. strcat(SQL::upd_form, SQL::upd_form2);
  283. }
  284. strcat(SQL::upd_form, " ");
  285. SQL::ReadCache[i] = mysql_query(connectionHandle, SQL::upd_form);
  286. if(mysql_errno() != 0)
  287. {
  288. return SQL_Warning("Query could not be completed due to error: %s", SQL::upd_form);
  289. }
  290. format(SQL::upd_query[i], SQL_MAX_QUERY_LENGTH, "%s", SQL::upd_form);
  291. SQL::upd_datacount[i] = 0;
  292. if(cache_is_valid(SQL::ReadCache[i]))
  293. {
  294. SQL::UsedHandle[i] = true;
  295. SQL::upd_connectionHandle[i] = connectionHandle;
  296. return i;
  297. }
  298. return SQL_INVALID_HANDLE;
  299. }
  300. static stock SQL::OpenTable_MultiReadEx2(const table[], const column_where[] = "", const row_identifier[] = "", const column_where2[] = "", const row_identifier2[] = "", row_identifier3 = -1, limit = -1, limit2 = -1, const desc[] = "", MySQL:connectionHandle = MYSQL_DEFAULT_HANDLE)
  301. {
  302. if(strlen(table) > SQL_MAX_TABLE_NAME)
  303. {
  304. SQL_Error("(SQL::OpenTable) Invalid table length.");
  305. return SQL_INVALID_HANDLE;
  306. }
  307. new
  308. i = SQL::GetFreeUpdatingSlot()
  309. ;
  310. if(i == SQL_INVALID_HANDLE)
  311. {
  312. return SQL_INVALID_HANDLE;
  313. }
  314. SQL::upd_type[i] = SQL::CALLBACK;
  315. format(SQL::upd_form, sizeof(SQL::upd_form), "SELECT * FROM `%s`", table);
  316. if(!isnull(column_where))
  317. {
  318. format(SQL::upd_form2, sizeof(SQL::upd_form2), " WHERE `%s`='%s'", column_where, row_identifier);
  319. strcat(SQL::upd_form, SQL::upd_form2);
  320. }
  321. if(!isnull(column_where2) && !isnull(row_identifier2) && row_identifier3 == -1)
  322. {
  323. format(SQL::upd_form2, sizeof(SQL::upd_form2), " AND `%s`='%s'", column_where2, row_identifier2);
  324. strcat(SQL::upd_form, SQL::upd_form2);
  325. }
  326. if(!isnull(column_where2) && row_identifier3 != -1 && isnull(row_identifier2))
  327. {
  328. format(SQL::upd_form2, sizeof(SQL::upd_form2), " AND `%s`='%d'", column_where2, row_identifier3);
  329. strcat(SQL::upd_form, SQL::upd_form2);
  330. }
  331. if(!isnull(desc))
  332. {
  333. format(SQL::upd_form2, sizeof(SQL::upd_form2), " ORDER BY `%s` DESC", desc);
  334. strcat(SQL::upd_form, SQL::upd_form2);
  335. }
  336. if(limit != -1 && limit2 != -1)
  337. {
  338. format(SQL::upd_form2, sizeof(SQL::upd_form2), " LIMIT %d, %d", limit, limit2);
  339. strcat(SQL::upd_form, SQL::upd_form2);
  340. }
  341. if(limit != -1 && limit2 == -1)
  342. {
  343. format(SQL::upd_form2, sizeof(SQL::upd_form2), " LIMIT %d", limit);
  344. strcat(SQL::upd_form, SQL::upd_form2);
  345. }
  346. strcat(SQL::upd_form, " ");
  347. SQL::ReadCache[i] = mysql_query(connectionHandle, SQL::upd_form);
  348. if(mysql_errno() != 0)
  349. {
  350. return SQL_Warning("Query could not be completed due to error: %s", SQL::upd_form);
  351. }
  352. format(SQL::upd_query[i], SQL_MAX_QUERY_LENGTH, "%s", SQL::upd_form);
  353. SQL::upd_datacount[i] = 0;
  354. if(cache_is_valid(SQL::ReadCache[i]))
  355. {
  356. SQL::UsedHandle[i] = true;
  357. SQL::upd_connectionHandle[i] = connectionHandle;
  358. return i;
  359. }
  360. return SQL_INVALID_HANDLE;
  361. }
  362. static stock SQL::OpenTable_MultiTableRead(const table[], MySQL:connectionHandle = MYSQL_DEFAULT_HANDLE)
  363. {
  364. if(strlen(table) > SQL_MAX_TABLE_NAME)
  365. {
  366. SQL_Error("(SQL::OpenTable) Invalid table length.");
  367. return SQL_INVALID_HANDLE;
  368. }
  369. new
  370. i = SQL::GetFreeUpdatingSlot()
  371. ;
  372. if(i == SQL_INVALID_HANDLE)
  373. {
  374. return SQL_INVALID_HANDLE;
  375. }
  376. SQL::upd_type[i] = SQL::CALLBACK;
  377. mysql_format(connectionHandle, SQL::upd_form, sizeof(SQL::upd_form), "SELECT * FROM `%s` ", table);
  378. SQL::ReadCache[i] = mysql_query(connectionHandle, SQL::upd_form);
  379. if(mysql_errno() != 0)
  380. {
  381. return SQL_Warning("Query could not be completed due to error: %s", SQL::upd_form);
  382. }
  383. format(SQL::upd_query[i], SQL_MAX_QUERY_LENGTH, "%s", SQL::upd_form);
  384. SQL::upd_datacount[i] = 0;
  385. if(cache_is_valid(SQL::ReadCache[i]))
  386. {
  387. SQL::UsedHandle[i] = true;
  388. SQL::upd_connectionHandle[i] = connectionHandle;
  389. return i;
  390. }
  391. return SQL_INVALID_HANDLE;
  392. }
  393. static stock SQL::CreateTable(const tablename[], MySQL:connectionHandle = MYSQL_DEFAULT_HANDLE)
  394. {
  395. if(strlen(tablename) > SQL_MAX_TABLE_NAME)
  396. {
  397. SQL_Error("(SQL::CreateTable) Invalid table length.");
  398. return SQL_INVALID_HANDLE;
  399. }
  400. new
  401. i = SQL::GetFreeUpdatingSlot()
  402. ;
  403. if(i == SQL_INVALID_HANDLE)
  404. {
  405. return SQL_INVALID_HANDLE;
  406. }
  407. SQL::upd_type[i] = SQL::CREATE;
  408. SQL::upd_connectionHandle[i] = connectionHandle;
  409. strcpy(SQL::upd_table[i], tablename);
  410. format(SQL::upd_query[i], SQL_MAX_QUERY_LENGTH, "CREATE TABLE %s (", SQL::upd_table[i]);
  411. SQL::upd_datacount[i] = 0;
  412. SQL::isset_primarykey[i] = 0;
  413. SQL::UsedHandle[i] = true;
  414. return i;
  415. }
  416. static stock SQL::OpenTable_Read(const table[], const column_where[] = "", row_identifier = -1, MySQL:connectionHandle = MYSQL_DEFAULT_HANDLE)
  417. {
  418. if(strlen(table) > SQL_MAX_TABLE_NAME)
  419. {
  420. SQL_Error("(SQL::OpenTable) Invalid table length.");
  421. return SQL_INVALID_HANDLE;
  422. }
  423. if(column_where[0] == '\0')
  424. {
  425. SQL_Error("(SQL::OpenTable) No format has been entered. Format example: (pID)");
  426. return SQL_INVALID_HANDLE;
  427. }
  428. new
  429. i = SQL::GetFreeUpdatingSlot()
  430. ;
  431. if(i == SQL_INVALID_HANDLE)
  432. {
  433. return SQL_INVALID_HANDLE;
  434. }
  435. SQL::upd_type[i] = SQL::READ;
  436. mysql_format(connectionHandle, SQL::upd_form, sizeof(SQL::upd_form), "SELECT * FROM `%s` WHERE `%s`='%d' ", table, column_where, row_identifier);
  437. SQL::ReadCache[i] = mysql_query(connectionHandle, SQL::upd_form);
  438. if(mysql_errno() != 0)
  439. {
  440. return SQL_Warning("Query could not be completed due to error: %s", SQL::upd_form);
  441. }
  442. format(SQL::upd_query[i], SQL_MAX_QUERY_LENGTH, "%s", SQL::upd_form);
  443. SQL::upd_datacount[i] = 0;
  444. if(cache_is_valid(SQL::ReadCache[i]))
  445. {
  446. SQL::UsedHandle[i] = true;
  447. SQL::upd_connectionHandle[i] = connectionHandle;
  448. return i;
  449. }
  450. return SQL_INVALID_HANDLE;
  451. }
  452. static stock SQL::OpenTable_Read2(const table[], const column_where[] = "", row_identifier = -1, const column_where2[] = "", row_identifier2 = -1, const row_identifier3[] = "", MySQL:connectionHandle = MYSQL_DEFAULT_HANDLE)
  453. {
  454. if(strlen(table) > SQL_MAX_TABLE_NAME)
  455. {
  456. SQL_Error("(SQL::OpenTable) Invalid table length.");
  457. return SQL_INVALID_HANDLE;
  458. }
  459. if(column_where[0] == '\0')
  460. {
  461. SQL_Error("(SQL::OpenTable) No format has been entered. Format example: (pID)");
  462. return SQL_INVALID_HANDLE;
  463. }
  464. if(column_where2[0] == '\0')
  465. {
  466. SQL_Error("(SQL::OpenTable) No format has been entered. Format example: (pID)");
  467. return SQL_INVALID_HANDLE;
  468. }
  469. new
  470. i = SQL::GetFreeUpdatingSlot()
  471. ;
  472. if(i == SQL_INVALID_HANDLE)
  473. {
  474. return SQL_INVALID_HANDLE;
  475. }
  476. SQL::upd_type[i] = SQL::READ;
  477. format(SQL::upd_form, sizeof(SQL::upd_form), "SELECT * FROM `%s` WHERE `%s`='%d'", table, column_where, row_identifier);
  478. if(!isnull(column_where2) && row_identifier2 != -1 && isnull(row_identifier3))
  479. {
  480. format(SQL::upd_form2, sizeof(SQL::upd_form2), " AND `%s`='%d'", column_where2, row_identifier2);
  481. strcat(SQL::upd_form, SQL::upd_form2);
  482. }
  483. if(!isnull(column_where2) && !isnull(row_identifier3) && row_identifier2 == -1)
  484. {
  485. format(SQL::upd_form2, sizeof(SQL::upd_form2), " AND `%s`='%s'", column_where2, row_identifier3);
  486. strcat(SQL::upd_form, SQL::upd_form2);
  487. }
  488. strcat(SQL::upd_form, " ");
  489. SQL::ReadCache[i] = mysql_query(connectionHandle, SQL::upd_form);
  490. if(mysql_errno() != 0)
  491. {
  492. return SQL_Warning("Query could not be completed due to error: %s", SQL::upd_form);
  493. }
  494. format(SQL::upd_query[i], SQL_MAX_QUERY_LENGTH, "%s", SQL::upd_form);
  495. SQL::upd_datacount[i] = 0;
  496. if(cache_is_valid(SQL::ReadCache[i]))
  497. {
  498. SQL::UsedHandle[i] = true;
  499. SQL::upd_connectionHandle[i] = connectionHandle;
  500. return i;
  501. }
  502. return SQL_INVALID_HANDLE;
  503. }
  504. static stock SQL::OpenTable_ReadEx(const table[], const column_where[] = "", const row_identifier[] = "", MySQL:connectionHandle = MYSQL_DEFAULT_HANDLE)
  505. {
  506. if(strlen(table) > SQL_MAX_TABLE_NAME)
  507. {
  508. SQL_Error("(SQL::OpenTable_ReadEx) Invalid table length.");
  509. return SQL_INVALID_HANDLE;
  510. }
  511. if(column_where[0] == '\0')
  512. {
  513. SQL_Error("(SQL::OpenTable_ReadEx) No format has been entered. Format example: (pID)");
  514. return SQL_INVALID_HANDLE;
  515. }
  516. new
  517. i = SQL::GetFreeUpdatingSlot()
  518. ;
  519. if(i == SQL_INVALID_HANDLE)
  520. {
  521. return SQL_INVALID_HANDLE;
  522. }
  523. SQL::upd_type[i] = SQL::READ;
  524. mysql_format(connectionHandle, SQL::upd_form, sizeof(SQL::upd_form), "SELECT * FROM `%s` WHERE `%s`='%s' ", table, column_where, row_identifier);
  525. SQL::ReadCache[i] = mysql_query(connectionHandle, SQL::upd_form);
  526. if(mysql_errno() != 0)
  527. {
  528. return SQL_Warning("Query could not be completed due to error: %s", SQL::upd_form);
  529. }
  530. format(SQL::upd_query[i], SQL_MAX_QUERY_LENGTH, "%s", SQL::upd_form);
  531. SQL::upd_datacount[i] = 0;
  532. if(cache_is_valid(SQL::ReadCache[i]))
  533. {
  534. SQL::UsedHandle[i] = true;
  535. SQL::upd_connectionHandle[i] = connectionHandle;
  536. return i;
  537. }
  538. return SQL_INVALID_HANDLE;
  539. }
  540. static stock SQL::OpenTable_ReadEx2(const table[], const column_where[] = "", const row_identifier[] = "", const column_where2[] = "", const row_identifier2[] = "", row_identifier3 = -1, MySQL:connectionHandle = MYSQL_DEFAULT_HANDLE)
  541. {
  542. if(strlen(table) > SQL_MAX_TABLE_NAME)
  543. {
  544. SQL_Error("(SQL::OpenTable_ReadEx) Invalid table length.");
  545. return SQL_INVALID_HANDLE;
  546. }
  547. if(column_where[0] == '\0')
  548. {
  549. SQL_Error("(SQL::OpenTable_ReadEx) No format has been entered. Format example: (pID)");
  550. return SQL_INVALID_HANDLE;
  551. }
  552. if(column_where2[0] == '\0')
  553. {
  554. SQL_Error("(SQL::OpenTable_ReadEx) No format has been entered. Format example: (pID)");
  555. return SQL_INVALID_HANDLE;
  556. }
  557. new
  558. i = SQL::GetFreeUpdatingSlot()
  559. ;
  560. if(i == SQL_INVALID_HANDLE)
  561. {
  562. return SQL_INVALID_HANDLE;
  563. }
  564. SQL::upd_type[i] = SQL::READ;
  565. format(SQL::upd_form, sizeof(SQL::upd_form), "SELECT * FROM `%s` WHERE `%s`='%s'", table, column_where, row_identifier);
  566. if(!isnull(column_where2) && !isnull(row_identifier2) && row_identifier3 == -1)
  567. {
  568. format(SQL::upd_form2, sizeof(SQL::upd_form2), " AND `%s`='%s'", column_where2, row_identifier2);
  569. strcat(SQL::upd_form, SQL::upd_form2);
  570. }
  571. if(!isnull(column_where2) && row_identifier3 != -1 && isnull(row_identifier2))
  572. {
  573. format(SQL::upd_form2, sizeof(SQL::upd_form2), " AND `%s`='%d'", column_where2, row_identifier3);
  574. strcat(SQL::upd_form, SQL::upd_form2);
  575. }
  576. strcat(SQL::upd_form, " ");
  577. SQL::ReadCache[i] = mysql_query(connectionHandle, SQL::upd_form);
  578. if(mysql_errno() != 0)
  579. {
  580. return SQL_Warning("Query could not be completed due to error: %s", SQL::upd_form);
  581. }
  582. format(SQL::upd_query[i], SQL_MAX_QUERY_LENGTH, "%s", SQL::upd_form);
  583. SQL::upd_datacount[i] = 0;
  584. if(cache_is_valid(SQL::ReadCache[i]))
  585. {
  586. SQL::UsedHandle[i] = true;
  587. SQL::upd_connectionHandle[i] = connectionHandle;
  588. return i;
  589. }
  590. return SQL_INVALID_HANDLE;
  591. }
  592. static stock SQL::OpenTable_TableRead(const table[], MySQL:connectionHandle = MYSQL_DEFAULT_HANDLE)
  593. {
  594. if(strlen(table) > SQL_MAX_TABLE_NAME)
  595. {
  596. SQL_Error("(SQL::OpenTable) Invalid table length.");
  597. return SQL_INVALID_HANDLE;
  598. }
  599. new
  600. i = SQL::GetFreeUpdatingSlot()
  601. ;
  602. if(i == SQL_INVALID_HANDLE)
  603. {
  604. return SQL_INVALID_HANDLE;
  605. }
  606. SQL::upd_type[i] = SQL::READ;
  607. mysql_format(connectionHandle, SQL::upd_form, sizeof(SQL::upd_form), "SELECT * FROM `%s` ", table);
  608. SQL::ReadCache[i] = mysql_query(connectionHandle, SQL::upd_form);
  609. if(mysql_errno() != 0)
  610. {
  611. return SQL_Warning("Query could not be completed due to error: %s", SQL::upd_form);
  612. }
  613. format(SQL::upd_query[i], SQL_MAX_QUERY_LENGTH, "%s", SQL::upd_form);
  614. SQL::upd_datacount[i] = 0;
  615. if(cache_is_valid(SQL::ReadCache[i]))
  616. {
  617. SQL::UsedHandle[i] = true;
  618. SQL::upd_connectionHandle[i] = connectionHandle;
  619. return i;
  620. }
  621. return SQL_INVALID_HANDLE;
  622. }
  623. static stock SQL::OpenTable_TableUpdate(const table[], MySQL:connectionHandle = MYSQL_DEFAULT_HANDLE)
  624. {
  625. if(strlen(table) > SQL_MAX_TABLE_NAME)
  626. {
  627. SQL_Error("(SQL::OpenTable) Invalid table length.");
  628. return SQL_INVALID_HANDLE;
  629. }
  630. new
  631. i = SQL::GetFreeUpdatingSlot()
  632. ;
  633. if(i == SQL_INVALID_HANDLE)
  634. {
  635. return SQL_INVALID_HANDLE;
  636. }
  637. SQL::upd_type[i] = SQL::TUPDATE;
  638. strcpy(SQL::upd_table[i], table);
  639. format(SQL::upd_query[i], SQL_MAX_QUERY_LENGTH, "UPDATE `%s` SET ", SQL::upd_table[i]);
  640. SQL::upd_datacount[i] = 0;
  641. SQL::UsedHandle[i] = true;
  642. SQL::upd_connectionHandle[i] = connectionHandle;
  643. return i;
  644. }
  645. static stock SQL::OpenTable_Update(const table[], const column_where[] = "", row_identifier = -1, MySQL:connectionHandle = MYSQL_DEFAULT_HANDLE)
  646. {
  647. if(strlen(table) > SQL_MAX_TABLE_NAME)
  648. {
  649. SQL_Error("(SQL::OpenTable) Invalid table length.");
  650. return SQL_INVALID_HANDLE;
  651. }
  652. if(column_where[0] == '\0')
  653. {
  654. SQL_Error("(SQL::OpenTable) No format has been entered. Format example: (pID)");
  655. return SQL_INVALID_HANDLE;
  656. }
  657. new
  658. i = SQL::GetFreeUpdatingSlot()
  659. ;
  660. if(i == SQL_INVALID_HANDLE)
  661. {
  662. return SQL_INVALID_HANDLE;
  663. }
  664. SQL::upd_type[i] = SQL::UPDATE;
  665. strcpy(SQL::upd_table[i], table);
  666. format(SQL::upd_query[i], SQL_MAX_QUERY_LENGTH, "UPDATE `%s` SET ", SQL::upd_table[i]);
  667. format(SQL::upd_rowidentifier[i], SQL_FORM_LENGTH, " WHERE `%s`='%d' ", column_where, row_identifier);
  668. SQL::upd_datacount[i] = 0;
  669. SQL::UsedHandle[i] = true;
  670. SQL::upd_connectionHandle[i] = connectionHandle;
  671. return i;
  672. }
  673. static stock SQL::OpenTable_Update2(const table[], const column_where[] = "", row_identifier = -1, const column_where2[] = "", row_identifier2 = -1, const row_identifier3[] = "", MySQL:connectionHandle = MYSQL_DEFAULT_HANDLE)
  674. {
  675. if(strlen(table) > SQL_MAX_TABLE_NAME)
  676. {
  677. SQL_Error("(SQL::OpenTable) Invalid table length.");
  678. return SQL_INVALID_HANDLE;
  679. }
  680. if(column_where[0] == '\0')
  681. {
  682. SQL_Error("(SQL::OpenTable) No format has been entered. Format example: (pID)");
  683. return SQL_INVALID_HANDLE;
  684. }
  685. if(column_where2[0] == '\0')
  686. {
  687. SQL_Error("(SQL::OpenTable) No format has been entered. Format example: (pID)");
  688. return SQL_INVALID_HANDLE;
  689. }
  690. new
  691. i = SQL::GetFreeUpdatingSlot()
  692. ;
  693. if(i == SQL_INVALID_HANDLE)
  694. {
  695. return SQL_INVALID_HANDLE;
  696. }
  697. SQL::upd_type[i] = SQL::UPDATE;
  698. strcpy(SQL::upd_table[i], table);
  699. format(SQL::upd_query[i], SQL_MAX_QUERY_LENGTH, "UPDATE `%s` SET ", SQL::upd_table[i]);
  700. format(SQL::upd_rowidentifier[i], SQL_FORM_LENGTH, " WHERE `%s`='%d'", column_where, row_identifier);
  701. if(!isnull(column_where2) && row_identifier2 != -1 && isnull(row_identifier3))
  702. {
  703. format(SQL::upd_form2, sizeof(SQL::upd_form2), " AND `%s`='%d'", column_where2, row_identifier2);
  704. strcat(SQL::upd_rowidentifier[i], SQL::upd_form2);
  705. }
  706. if(!isnull(column_where2) && !isnull(row_identifier3) && row_identifier2 == -1)
  707. {
  708. format(SQL::upd_form2, sizeof(SQL::upd_form2), " AND `%s`='%s'", column_where2, row_identifier3);
  709. strcat(SQL::upd_rowidentifier[i], SQL::upd_form2);
  710. }
  711. strcat(SQL::upd_rowidentifier[i], " ");
  712. SQL::upd_datacount[i] = 0;
  713. SQL::UsedHandle[i] = true;
  714. SQL::upd_connectionHandle[i] = connectionHandle;
  715. return i;
  716. }
  717. static stock SQL::OpenTable_UpdateEx(const table[], const column_where[] = "", const row_identifier[] = "", MySQL:connectionHandle = MYSQL_DEFAULT_HANDLE)
  718. {
  719. if(strlen(table) > SQL_MAX_TABLE_NAME)
  720. {
  721. SQL_Error("(SQL::OpenTable_UpdateEx) Invalid table length.");
  722. return SQL_INVALID_HANDLE;
  723. }
  724. if(column_where[0] == '\0')
  725. {
  726. SQL_Error("(SQL::OpenTable_UpdateEx) No format has been entered. Format example: (pID)");
  727. return SQL_INVALID_HANDLE;
  728. }
  729. new
  730. i = SQL::GetFreeUpdatingSlot()
  731. ;
  732. if(i == SQL_INVALID_HANDLE)
  733. {
  734. return SQL_INVALID_HANDLE;
  735. }
  736. SQL::upd_type[i] = SQL::UPDATE;
  737. strcpy(SQL::upd_table[i], table);
  738. format(SQL::upd_query[i], SQL_MAX_QUERY_LENGTH, "UPDATE `%s` SET ", SQL::upd_table[i]);
  739. format(SQL::upd_rowidentifier[i], SQL_FORM_LENGTH, " WHERE `%s`='%s' ", column_where, row_identifier);
  740. SQL::upd_datacount[i] = 0;
  741. SQL::UsedHandle[i] = true;
  742. SQL::upd_connectionHandle[i] = connectionHandle;
  743. return i;
  744. }
  745. static stock SQL::OpenTable_UpdateEx2(const table[], const column_where[] = "", const row_identifier[] = "", const column_where2[] = "", const row_identifier2[] = "", row_identifier3 = -1, MySQL:connectionHandle = MYSQL_DEFAULT_HANDLE)
  746. {
  747. if(strlen(table) > SQL_MAX_TABLE_NAME)
  748. {
  749. SQL_Error("(SQL::OpenTable_UpdateEx) Invalid table length.");
  750. return SQL_INVALID_HANDLE;
  751. }
  752. if(column_where[0] == '\0')
  753. {
  754. SQL_Error("(SQL::OpenTable_UpdateEx) No format has been entered. Format example: (pID)");
  755. return SQL_INVALID_HANDLE;
  756. }
  757. if(column_where2[0] == '\0')
  758. {
  759. SQL_Error("(SQL::OpenTable_UpdateEx) No format has been entered. Format example: (pID)");
  760. return SQL_INVALID_HANDLE;
  761. }
  762. new
  763. i = SQL::GetFreeUpdatingSlot()
  764. ;
  765. if(i == SQL_INVALID_HANDLE)
  766. {
  767. return SQL_INVALID_HANDLE;
  768. }
  769. SQL::upd_type[i] = SQL::UPDATE;
  770. strcpy(SQL::upd_table[i], table);
  771. format(SQL::upd_query[i], SQL_MAX_QUERY_LENGTH, "UPDATE `%s` SET ", SQL::upd_table[i]);
  772. format(SQL::upd_rowidentifier[i], SQL_FORM_LENGTH, " WHERE `%s`='%s'", column_where, row_identifier);
  773. if(!isnull(column_where2) && !isnull(row_identifier2) && row_identifier3 == -1)
  774. {
  775. format(SQL::upd_form2, sizeof(SQL::upd_form2), " AND `%s`='%s'", column_where2, row_identifier2);
  776. strcat(SQL::upd_rowidentifier[i], SQL::upd_form2);
  777. }
  778. if(!isnull(column_where2) && row_identifier3 != -1 && isnull(row_identifier2))
  779. {
  780. format(SQL::upd_form2, sizeof(SQL::upd_form2), " AND `%s`='%d'", column_where2, row_identifier3);
  781. strcat(SQL::upd_rowidentifier[i], SQL::upd_form2);
  782. }
  783. strcat(SQL::upd_rowidentifier[i], " ");
  784. SQL::upd_datacount[i] = 0;
  785. SQL::UsedHandle[i] = true;
  786. SQL::upd_connectionHandle[i] = connectionHandle;
  787. return i;
  788. }
  789. static stock SQL::UpdateFloatEntry(handle, const field[], Float:value)
  790. {
  791. if(!SQL::IsValidUpdatingSlot(handle))
  792. {
  793. SQL_Error("(SQL::UpdateFloatEntry) Invalid handle. Make sure you used (SQL::OpenTable) first.");
  794. return 0;
  795. }
  796. if(SQL::upd_type[handle] != SQL::UPDATE && SQL::upd_type[handle] != SQL::TUPDATE) return 0;
  797. format(SQL::upd_form, sizeof(SQL::upd_form), "`%s`='%f',", field, value);
  798. strcat(SQL::upd_query[handle], SQL::upd_form);
  799. SQL::upd_datacount[handle]++;
  800. return 1;
  801. }
  802. static stock SQL::UpdateStringEntry(handle, const field[], const value[], bool:use_real_escape = true)
  803. {
  804. if(!SQL::IsValidUpdatingSlot(handle))
  805. {
  806. SQL_Error("(SQL::UpdateStringEntry) Invalid handle. Make sure you used (SQL::OpenTable) first.");
  807. return 0;
  808. }
  809. if(SQL::upd_type[handle] != SQL::UPDATE && SQL::upd_type[handle] != SQL::TUPDATE) return 0;
  810. if(use_real_escape == true)
  811. {
  812. new escape[SQL_FORM_LENGTH];
  813. mysql_escape_string(value, escape);
  814. format(SQL::upd_form, sizeof(SQL::upd_form), "`%s`='%s',", field, escape);
  815. }
  816. else
  817. {
  818. format(SQL::upd_form, sizeof(SQL::upd_form), "`%s`='%s',", field, value);
  819. }
  820. strcat(SQL::upd_query[handle], SQL::upd_form);
  821. SQL::upd_datacount[handle]++;
  822. return 1;
  823. }
  824. static stock SQL::OpenTable_Insert(const table[], bool:use_autoincrement = false, MySQL:connectionHandle = MYSQL_DEFAULT_HANDLE)
  825. {
  826. if(strlen(table) > SQL_MAX_TABLE_NAME)
  827. {
  828. SQL_Error("(SQL::OpenTable_Insert) Invalid table length.");
  829. return SQL_INVALID_HANDLE;
  830. }
  831. new
  832. i = SQL::GetFreeUpdatingSlot()
  833. ;
  834. if(i == SQL_INVALID_HANDLE)
  835. {
  836. return SQL_INVALID_HANDLE;
  837. }
  838. SQL::upd_type[i] = SQL::INSERT;
  839. strcpy(SQL::upd_table[i], table);
  840. format(SQL::upd_query[i], SQL_MAX_QUERY_LENGTH, "INSERT INTO `%s` (", SQL::upd_table[i]);
  841. strcpy(SQL::upd_query_2[i], " VALUES (");
  842. SQL::upd_datacount[i] = 0;
  843. if(use_autoincrement == true)
  844. {
  845. SQL::upd_useautoincrement[i] = true;
  846. }
  847. else
  848. {
  849. SQL::upd_useautoincrement[i] = false;
  850. }
  851. SQL::upd_connectionHandle[i] = connectionHandle;
  852. SQL::UsedHandle[i] = true;
  853. return i;
  854. }
  855. static stock SQL::InsertIntEntry(handle, const field[], value)
  856. {
  857. if(!SQL::IsValidUpdatingSlot(handle))
  858. {
  859. SQL_Error("(SQL::UpdateIntEntry) Invalid handle. Make sure you used (SQL::OpenTable) first.");
  860. return 0;
  861. }
  862. if(SQL::upd_type[handle] != SQL::INSERT) return 0;
  863. format(SQL::upd_form, sizeof(SQL::upd_form), "`%s`,", field);
  864. strcat(SQL::upd_query[handle], SQL::upd_form);
  865. format(SQL::upd_form, sizeof(SQL::upd_form), "'%i',", value);
  866. strcat(SQL::upd_query_2[handle], SQL::upd_form);
  867. SQL::upd_datacount[handle]++;
  868. return 1;
  869. }
  870. static stock SQL::InsertFloatEntry(handle, const field[], Float:value)
  871. {
  872. if(!SQL::IsValidUpdatingSlot(handle))
  873. {
  874. SQL_Error("(SQL::UpdateIntEntry) Invalid handle. Make sure you used (SQL::OpenTable) first.");
  875. return 0;
  876. }
  877. if(SQL::upd_type[handle] != SQL::INSERT) return 0;
  878. format(SQL::upd_form, sizeof(SQL::upd_form), "`%s`,", field);
  879. strcat(SQL::upd_query[handle], SQL::upd_form);
  880. format(SQL::upd_form, sizeof(SQL::upd_form), "'%f',", value);
  881. strcat(SQL::upd_query_2[handle], SQL::upd_form);
  882. SQL::upd_datacount[handle]++;
  883. return 1;
  884. }
  885. static stock SQL::InsertStringEntry(handle, const field[], const value[], bool:use_real_escape = true)
  886. {
  887. if(!SQL::IsValidUpdatingSlot(handle))
  888. {
  889. SQL_Error("(SQL::UpdateIntEntry) Invalid handle. Make sure you used (SQL::OpenTable) first.");
  890. return 0;
  891. }
  892. if(SQL::upd_type[handle] != SQL::INSERT) return 0;
  893. format(SQL::upd_form, sizeof(SQL::upd_form), "`%s`,", field);
  894. strcat(SQL::upd_query[handle], SQL::upd_form);
  895. if(use_real_escape == true)
  896. {
  897. new escape[SQL_FORM_LENGTH];
  898. mysql_escape_string(value, escape);
  899. format(SQL::upd_form, sizeof(SQL::upd_form), "'%s',", escape);
  900. }
  901. else
  902. {
  903. format(SQL::upd_form, sizeof(SQL::upd_form), "'%s',", value);
  904. }
  905. strcat(SQL::upd_query_2[handle], SQL::upd_form);
  906. SQL::upd_datacount[handle]++;
  907. return 1;
  908. }
  909. /*==============================================================================
  910. Actual functions
  911. ================================================================================*/
  912. #define SQL_TReadRetrievedRows(%0,%1) if(IsMultiReadHandleValid(%0)) if(cache_num_rows()) for(new %1 = 0; %1 < cache_num_rows(); %1++)
  913. stock SQL::cache_get_value_name_int(row_idx, const column_where[])
  914. {
  915. new int = 0;
  916. cache_get_value_name_int(row_idx, column_where, int);
  917. return int;
  918. }
  919. stock Float:SQL::cache_get_value_name_float(row_idx, const column_where[])
  920. {
  921. new Float:fval = 0;
  922. cache_get_value_name_float(row_idx, column_where, fval);
  923. return fval;
  924. }
  925. stock MySQL:SQL::Connect(const host[], const user[], const password[], const database[], debugging = 0, port = 3306, bool:autoreconnect = true, pool_size = 2)
  926. {
  927. switch(debugging)
  928. {
  929. case 0: mysql_log(NONE);
  930. case 1: mysql_log(ALL);
  931. case 2: mysql_log(ERROR);
  932. case 3: mysql_log(WARNING);
  933. case 4: mysql_log(INFO);
  934. case 5: mysql_log(DEBUG);
  935. case 6: mysql_log(ERROR | WARNING);
  936. case 7: mysql_log(ERROR | DEBUG);
  937. case 8: mysql_log(ERROR | INFO);
  938. case 9: mysql_log(WARNING | DEBUG);
  939. case 10: mysql_log(WARNING | INFO);
  940. case 11: mysql_log(INFO | DEBUG);
  941. case 12: mysql_log(ERROR | WARNING | DEBUG);
  942. case 13: mysql_log(ERROR | WARNING | DEBUG | INFO);
  943. case 14: mysql_log(WARNING | DEBUG | INFO);
  944. }
  945. new MySQLOpt:options = mysql_init_options();
  946. mysql_set_option(options, SERVER_PORT, port);
  947. mysql_set_option(options, AUTO_RECONNECT, autoreconnect);
  948. mysql_set_option(options, POOL_SIZE, pool_size);
  949. new MySQL:SQL::mc = mysql_connect(host, user, password, database, options);
  950. if(mysql_errno(SQL::mc) != 0)
  951. {
  952. SQL_Warning("Could not connect to database %s on host %s | user %s and password %s", database, host, user, password);
  953. }
  954. else
  955. {
  956. SQL_Notice("Successfully connected to database!");
  957. }
  958. return SQL::mc;
  959. }
  960. stock SQL::Disconnect(MySQL:handle = MYSQL_DEFAULT_HANDLE)
  961. {
  962. return mysql_close(handle);
  963. }
  964. stock SQL::Begin(MySQL:connectionHandle = MYSQL_DEFAULT_HANDLE)
  965. {
  966. mysql_tquery(connectionHandle, "BEGIN", "", "");
  967. return 1;
  968. }
  969. stock SQL::Commit(MySQL:connectionHandle = MYSQL_DEFAULT_HANDLE)
  970. {
  971. mysql_tquery(connectionHandle, "COMMIT", "", "");
  972. return 1;
  973. }
  974. stock SQL::DeleteRow(const table[], const column_where[] = "", row_identifier = -1, limit = -1, limit2 = -1, MySQL:connectionHandle = MYSQL_DEFAULT_HANDLE)
  975. {
  976. format(SQL::upd_form, sizeof(SQL::upd_form), "DELETE FROM `%s` WHERE `%s`='%d'", table, column_where, row_identifier);
  977. if(limit != -1 && limit2 != -1)
  978. {
  979. format(SQL::upd_form2, sizeof(SQL::upd_form2), " LIMIT %d, %d", limit, limit2);
  980. strcat(SQL::upd_form, SQL::upd_form2);
  981. }
  982. if(limit != -1 && limit2 == -1)
  983. {
  984. format(SQL::upd_form2, sizeof(SQL::upd_form2), " LIMIT %d", limit);
  985. strcat(SQL::upd_form, SQL::upd_form2);
  986. }
  987. strcat(SQL::upd_form, " ");
  988. mysql_tquery(connectionHandle, SQL::upd_form, "", "");
  989. return 1;
  990. }
  991. stock SQL::DeleteRow2(const table[], const column_where[] = "", row_identifier = -1, const column_where2[] = "", row_identifier2 = -1, const row_identifier3[] = "", limit = -1, limit2 = -1, MySQL:connectionHandle = MYSQL_DEFAULT_HANDLE)
  992. {
  993. format(SQL::upd_form, sizeof(SQL::upd_form), "DELETE FROM `%s` WHERE `%s`='%d'", table, column_where, row_identifier);
  994. if(!isnull(column_where2) && row_identifier2 != -1 && isnull(row_identifier3))
  995. {
  996. format(SQL::upd_form2, sizeof(SQL::upd_form2), " AND `%s`='%d'", column_where2, row_identifier2);
  997. strcat(SQL::upd_form, SQL::upd_form2);
  998. }
  999. if(!isnull(column_where2) && !isnull(row_identifier3) && row_identifier2 == -1)
  1000. {
  1001. format(SQL::upd_form2, sizeof(SQL::upd_form2), " AND `%s`='%s'", column_where2, row_identifier3);
  1002. strcat(SQL::upd_form, SQL::upd_form2);
  1003. }
  1004. if(limit != -1 && limit2 != -1)
  1005. {
  1006. format(SQL::upd_form2, sizeof(SQL::upd_form2), " LIMIT %d, %d", limit, limit2);
  1007. strcat(SQL::upd_form, SQL::upd_form2);
  1008. }
  1009. if(limit != -1 && limit2 == -1)
  1010. {
  1011. format(SQL::upd_form2, sizeof(SQL::upd_form2), " LIMIT %d", limit);
  1012. strcat(SQL::upd_form, SQL::upd_form2);
  1013. }
  1014. strcat(SQL::upd_form, " ");
  1015. mysql_tquery(connectionHandle, SQL::upd_form, "", "");
  1016. return 1;
  1017. }
  1018. stock SQL::DeleteRow3(const table[], MySQL:connectionHandle, const column_where[] = "", type = 'i', {Float,_}:...)
  1019. {
  1020. switch (type)
  1021. {
  1022. case 'd', 'i': mysql_format(connectionHandle, SQL::upd_form, sizeof(SQL::upd_form), "DELETE FROM `%s` WHERE `%s`='%d' ", table, column_where, getarg(4));
  1023. case 's':
  1024. {
  1025. new i_ArgLoop = -1;
  1026. new i_Extract[128];
  1027. while (++i_ArgLoop != 0x100)
  1028. {
  1029. if(i_ArgLoop < 128)
  1030. {
  1031. i_Extract[i_ArgLoop] = getarg(4, i_ArgLoop);
  1032. }
  1033. }
  1034. mysql_format(connectionHandle, SQL::upd_form, sizeof(SQL::upd_form), "DELETE FROM `%s` WHERE `%s`='%e' ", table, column_where, i_Extract);
  1035. }
  1036. case 'f': mysql_format(connectionHandle, SQL::upd_form, sizeof(SQL::upd_form), "DELETE FROM `%s` WHERE `%s`='%f' ", table, column_where, getarg(4));
  1037. }
  1038. mysql_tquery(connectionHandle, SQL::upd_form, "", "");
  1039. return 1;
  1040. }
  1041. stock SQL::Open(SQL::qtypes:type, const table[], const column_where[] = "", row_identifier = -1, const column_where2[] = "", row_identifier2 = -1, const row_identifier3[] = "", limit = -1, limit2 = -1, const desc[] = "", MySQL:connectionHandle = MYSQL_DEFAULT_HANDLE)
  1042. {
  1043. new handle;
  1044. switch(type)
  1045. {
  1046. case SQL::CREATE:
  1047. {
  1048. handle = SQL::CreateTable(table, connectionHandle);
  1049. }
  1050. case SQL::READ:
  1051. {
  1052. handle = SQL::OpenTable_Read(table, column_where, row_identifier, connectionHandle);
  1053. }
  1054. case SQL::READ2:
  1055. {
  1056. handle = SQL::OpenTable_Read2(table, column_where, row_identifier, column_where2, row_identifier2, row_identifier3, connectionHandle);
  1057. }
  1058. case SQL::TREAD:
  1059. {
  1060. handle = SQL::OpenTable_TableRead(table, connectionHandle);
  1061. }
  1062. case SQL::INSERT:
  1063. {
  1064. handle = SQL::OpenTable_Insert(table, false, connectionHandle);
  1065. }
  1066. case SQL::UPDATE:
  1067. {
  1068. handle = SQL::OpenTable_Update(table, column_where, row_identifier, connectionHandle);
  1069. }
  1070. case SQL::UPDATE2:
  1071. {
  1072. handle = SQL::OpenTable_Update2(table, column_where, row_identifier, column_where2, row_identifier2, row_identifier3, connectionHandle);
  1073. }
  1074. case SQL::TUPDATE:
  1075. {
  1076. handle = SQL::OpenTable_TableUpdate(table, connectionHandle);
  1077. }
  1078. case SQL::MREAD:
  1079. {
  1080. handle = SQL::OpenTable_MultiRead(table, column_where, row_identifier, limit, limit2, desc, connectionHandle);
  1081. }
  1082. case SQL::MREAD2:
  1083. {
  1084. handle = SQL::OpenTable_MultiRead2(table, column_where, row_identifier, column_where2, row_identifier2, row_identifier3, limit, limit2, desc, connectionHandle);
  1085. }
  1086. case SQL::MTREAD:
  1087. {
  1088. handle = SQL::OpenTable_MultiTableRead(table, connectionHandle);
  1089. }
  1090. }
  1091. return handle;
  1092. }
  1093. stock SQL::OpenEx(SQL::qtypes:type, const table[], const column_where[] = "", const row_identifier[] = "", const column_where2[] = "", const row_identifier2[] = "", row_identifier3 = -1, limit = -1, limit2 = -1, const desc[] = "", MySQL:connectionHandle = MYSQL_DEFAULT_HANDLE)
  1094. {
  1095. new handle;
  1096. switch(type)
  1097. {
  1098. case SQL::CREATE:
  1099. {
  1100. handle = SQL::CreateTable(table, connectionHandle);
  1101. }
  1102. case SQL::READ:
  1103. {
  1104. handle = SQL::OpenTable_ReadEx(table, column_where, row_identifier, connectionHandle);
  1105. }
  1106. case SQL::READ2:
  1107. {
  1108. handle = SQL::OpenTable_ReadEx2(table, column_where, row_identifier, column_where2, row_identifier2, row_identifier3, connectionHandle);
  1109. }
  1110. case SQL::TREAD:
  1111. {
  1112. handle = SQL::OpenTable_TableRead(table, connectionHandle);
  1113. }
  1114. case SQL::INSERT:
  1115. {
  1116. handle = SQL::OpenTable_Insert(table, false, connectionHandle);
  1117. }
  1118. case SQL::UPDATE:
  1119. {
  1120. handle = SQL::OpenTable_UpdateEx(table, column_where, row_identifier, connectionHandle);
  1121. }
  1122. case SQL::UPDATE2:
  1123. {
  1124. handle = SQL::OpenTable_UpdateEx2(table, column_where, row_identifier, column_where2, row_identifier2, row_identifier3, connectionHandle);
  1125. }
  1126. case SQL::TUPDATE:
  1127. {
  1128. handle = SQL::OpenTable_TableUpdate(table, connectionHandle);
  1129. }
  1130. case SQL::MREAD:
  1131. {
  1132. handle = SQL::OpenTable_MultiReadEx(table, column_where, row_identifier, limit, limit2, desc, connectionHandle);
  1133. }
  1134. case SQL::MREAD2:
  1135. {
  1136. handle = SQL::OpenTable_MultiReadEx2(table, column_where, row_identifier, column_where2, row_identifier2, row_identifier3, limit, limit2, desc, connectionHandle);
  1137. }
  1138. case SQL::MTREAD:
  1139. {
  1140. handle = SQL::OpenTable_MultiTableRead(table, connectionHandle);
  1141. }
  1142. }
  1143. return handle;
  1144. }
  1145. stock SQL::ToggleAutoIncrement(handle, bool:toggle)
  1146. {
  1147. if(!SQL::IsValidUpdatingSlot(handle)) return 0;
  1148. if(SQL::upd_type[handle] != SQL::INSERT) return 0;
  1149. SQL::upd_useautoincrement[handle] = toggle;
  1150. return 1;
  1151. }
  1152. stock SQL::DeleteRowEx(const table[], const column_where[] = "", const row_identifier[] = "", limit = -1, limit2 = -1, MySQL:connectionHandle = MYSQL_DEFAULT_HANDLE)
  1153. {
  1154. mysql_format(connectionHandle, SQL::upd_form, sizeof(SQL::upd_form), "DELETE FROM `%s` WHERE `%s`='%e'", table, column_where, row_identifier);
  1155. if(limit != -1 && limit2 != -1)
  1156. {
  1157. format(SQL::upd_form2, sizeof(SQL::upd_form2), " LIMIT %d, %d", limit, limit2);
  1158. strcat(SQL::upd_form, SQL::upd_form2);
  1159. }
  1160. if(limit != -1 && limit2 == -1)
  1161. {
  1162. format(SQL::upd_form2, sizeof(SQL::upd_form2), " LIMIT %d", limit);
  1163. strcat(SQL::upd_form, SQL::upd_form2);
  1164. }
  1165. strcat(SQL::upd_form, " ");
  1166. mysql_tquery(connectionHandle, SQL::upd_form, "", "");
  1167. return 1;
  1168. }
  1169. stock SQL::DeleteRowEx2(const table[], const column_where[] = "", const row_identifier[] = "", const column_where2[] = "", const row_identifier2[] = "", row_identifier3 = -1, limit = -1, limit2 = -1, MySQL:connectionHandle = MYSQL_DEFAULT_HANDLE)
  1170. {
  1171. format(SQL::upd_form, sizeof(SQL::upd_form), "DELETE FROM `%s` WHERE `%s`='%e'", table, column_where, row_identifier);
  1172. if(!isnull(column_where2) && !isnull(row_identifier2) && row_identifier3 == -1)
  1173. {
  1174. format(SQL::upd_form2, sizeof(SQL::upd_form2), " AND `%s`='%e'", column_where2, row_identifier2);
  1175. strcat(SQL::upd_form, SQL::upd_form2);
  1176. }
  1177. if(!isnull(column_where2) && row_identifier3 != -1 && isnull(row_identifier2))
  1178. {
  1179. format(SQL::upd_form2, sizeof(SQL::upd_form2), " AND `%s`='%d'", column_where2, row_identifier3);
  1180. strcat(SQL::upd_form, SQL::upd_form2);
  1181. }
  1182. if(limit != -1 && limit2 != -1)
  1183. {
  1184. format(SQL::upd_form2, sizeof(SQL::upd_form2), " LIMIT %d, %d", limit, limit2);
  1185. strcat(SQL::upd_form, SQL::upd_form2);
  1186. }
  1187. if(limit != -1 && limit2 == -1)
  1188. {
  1189. format(SQL::upd_form2, sizeof(SQL::upd_form2), " LIMIT %d", limit);
  1190. strcat(SQL::upd_form, SQL::upd_form2);
  1191. }
  1192. strcat(SQL::upd_form, " ");
  1193. mysql_tquery(connectionHandle, SQL::upd_form, "", "");
  1194. return 1;
  1195. }
  1196. stock SQL::GetIntEntry(const table[], const field[], const column_where[] = "", row_identifier = -1, const column_whereo[] = "=", MySQL:connectionHandle = MYSQL_DEFAULT_HANDLE)
  1197. {
  1198. new Cache:result, int = 0, count;
  1199. format(SQL::upd_form, sizeof(SQL::upd_form), "SELECT `%s` FROM `%s` WHERE `%s`%s'%d' ", field, table, column_where, column_whereo, row_identifier);
  1200. result = mysql_query(connectionHandle, SQL::upd_form);
  1201. if(mysql_errno() != 0)
  1202. {
  1203. return SQL_Warning("Query could not be completed due to error: %s", SQL::upd_form);
  1204. }
  1205. if(result)
  1206. {
  1207. cache_get_row_count(count);
  1208. if(count) cache_get_value_name_int(0, field, int);
  1209. cache_delete(result);
  1210. }
  1211. return int;
  1212. }
  1213. stock SQL::GetIntEntry2(const table[], const field[], const column_where[] = "", row_identifier = -1, const column_where2[] = "", row_identifier2 = -1, const row_identifier3[] = "", const column_whereo[] = "=", const column_whereo2[] = "=", MySQL:connectionHandle = MYSQL_DEFAULT_HANDLE)
  1214. {
  1215. new Cache:result, int = 0, count;
  1216. format(SQL::upd_form, sizeof(SQL::upd_form), "SELECT `%s` FROM `%s` WHERE `%s`%s'%d'", field, table, column_where, column_whereo, row_identifier);
  1217. if(!isnull(column_where2) && row_identifier2 != -1 && isnull(row_identifier3))
  1218. {
  1219. format(SQL::upd_form2, sizeof(SQL::upd_form2), " AND `%s`%s'%d'", column_where2, column_whereo2, row_identifier2);
  1220. strcat(SQL::upd_form, SQL::upd_form2);
  1221. }
  1222. if(!isnull(column_where2) && !isnull(row_identifier3) && row_identifier2 == -1)
  1223. {
  1224. format(SQL::upd_form2, sizeof(SQL::upd_form2), " AND `%s`%s'%s'", column_where2, column_whereo2, row_identifier3);
  1225. strcat(SQL::upd_form, SQL::upd_form2);
  1226. }
  1227. strcat(SQL::upd_form, " ");
  1228. result = mysql_query(connectionHandle, SQL::upd_form);
  1229. if(mysql_errno() != 0)
  1230. {
  1231. return SQL_Warning("Query could not be completed due to error: %s", SQL::upd_form);
  1232. }
  1233. if(result)
  1234. {
  1235. cache_get_row_count(count);
  1236. if(count) cache_get_value_name_int(0, field, int);
  1237. cache_delete(result);
  1238. }
  1239. return int;
  1240. }
  1241. stock Float:SQL::GetFloatEntry(const table[], const field[], const column_where[] = "", row_identifier = -1, const column_whereo[] = "=", MySQL:connectionHandle = MYSQL_DEFAULT_HANDLE)
  1242. {
  1243. new
  1244. Cache:result,
  1245. Float:int,
  1246. count
  1247. ;
  1248. format(SQL::upd_form, sizeof(SQL::upd_form), "SELECT `%s` FROM `%s` WHERE `%s`%s'%d' ", field, table, column_where, column_whereo, row_identifier);
  1249. result = mysql_query(connectionHandle, SQL::upd_form);
  1250. if(mysql_errno() != 0)
  1251. {
  1252. return SQL_Warning("Query could not be completed due to error: %s", SQL::upd_form);
  1253. }
  1254. if(result)
  1255. {
  1256. cache_get_row_count(count);
  1257. if(count)
  1258. {
  1259. cache_get_value_name_float(0, field, int);
  1260. }
  1261. else
  1262. {
  1263. int = 0.0;
  1264. }
  1265. cache_delete(result);
  1266. }
  1267. return int;
  1268. }
  1269. stock Float:SQL::GetFloatEntry2(const table[], const field[], const column_where[] = "", row_identifier = -1, const column_where2[] = "", row_identifier2 = -1, const row_identifier3[] = "", const column_whereo[] = "=", const column_whereo2[] = "=", MySQL:connectionHandle = MYSQL_DEFAULT_HANDLE)
  1270. {
  1271. new
  1272. Cache:result,
  1273. Float:int,
  1274. count
  1275. ;
  1276. format(SQL::upd_form, sizeof(SQL::upd_form), "SELECT `%s` FROM `%s` WHERE `%s`%s'%d'", field, table, column_where, column_whereo, row_identifier);
  1277. if(!isnull(column_where2) && row_identifier2 != -1 && isnull(row_identifier3))
  1278. {
  1279. format(SQL::upd_form2, sizeof(SQL::upd_form2), " AND `%s`%s'%d'", column_where2, column_whereo2, row_identifier2);
  1280. strcat(SQL::upd_form, SQL::upd_form2);
  1281. }
  1282. if(!isnull(column_where2) && !isnull(row_identifier3) && row_identifier2 == -1)
  1283. {
  1284. format(SQL::upd_form2, sizeof(SQL::upd_form2), " AND `%s`%s'%s'", column_where2, column_whereo2, row_identifier3);
  1285. strcat(SQL::upd_form, SQL::upd_form2);
  1286. }
  1287. strcat(SQL::upd_form, " ");
  1288. result = mysql_query(connectionHandle, SQL::upd_form);
  1289. if(mysql_errno() != 0)
  1290. {
  1291. return SQL_Warning("Query could not be completed due to error: %s", SQL::upd_form);
  1292. }
  1293. if(result)
  1294. {
  1295. cache_get_row_count(count);
  1296. if(count)
  1297. {
  1298. cache_get_value_name_float(0, field, int);
  1299. }
  1300. else
  1301. {
  1302. int = 0.0;
  1303. }
  1304. cache_delete(result);
  1305. }
  1306. return int;
  1307. }
  1308. stock SQL::t_GetStringEntry(const table[], const field[], const column_where[] = "", row_identifier = -1, const column_whereo[] = "=", MySQL:connectionHandle = MYSQL_DEFAULT_HANDLE)
  1309. {
  1310. new Cache:result, count;
  1311. format(SQL::upd_form, sizeof(SQL::upd_form), "SELECT `%s` FROM `%s` WHERE `%s`%s'%d' ", field, table, column_where, column_whereo, row_identifier);
  1312. result = mysql_query(connectionHandle, SQL::upd_form);
  1313. if(mysql_errno() != 0)
  1314. {
  1315. return SQL_Warning("Query could not be completed due to error: %s", SQL::upd_form);
  1316. }
  1317. SQL::upd_form[0] = EOS;
  1318. if(result)
  1319. {
  1320. cache_get_row_count(count);
  1321. if(count == 1)
  1322. {
  1323. cache_get_value_name(0, field, SQL::upd_form, sizeof(SQL::upd_form));
  1324. cache_delete(result);
  1325. }
  1326. else
  1327. {
  1328. cache_delete(result);
  1329. return SQL::upd_form;
  1330. }
  1331. }
  1332. return SQL::upd_form;
  1333. }
  1334. stock SQL::t_GetStringEntry2(const table[], const field[], const column_where[] = "", row_identifier = -1, const column_where2[] = "", row_identifier2 = -1, const row_identifier3[] = "", const column_whereo[] = "=", const column_whereo2[] = "=", MySQL:connectionHandle = MYSQL_DEFAULT_HANDLE)
  1335. {
  1336. new Cache:result, count;
  1337. format(SQL::upd_form, sizeof(SQL::upd_form), "SELECT `%s` FROM `%s` WHERE `%s`%s'%d' ", field, table, column_where, column_whereo, row_identifier);
  1338. result = mysql_query(connectionHandle, SQL::upd_form);
  1339. if(mysql_errno() != 0)
  1340. {
  1341. return SQL_Warning("Query could not be completed due to error: %s", SQL::upd_form);
  1342. }
  1343. SQL::upd_form[0] = EOS;
  1344. if(result)
  1345. {
  1346. cache_get_row_count(count);
  1347. if(count == 1)
  1348. {
  1349. cache_get_value_name(0, field, SQL::upd_form, sizeof(SQL::upd_form));
  1350. cache_delete(result);
  1351. }
  1352. else
  1353. {
  1354. cache_delete(result);
  1355. return SQL::upd_form;
  1356. }
  1357. }
  1358. return SQL::upd_form;
  1359. }
  1360. stock SQL::GetStringEntry(const table[], const field[], const column_where[] = "", row_identifier = -1, dest[], len = sizeof(dest), const column_whereo[] = "=", MySQL:connectionHandle = MYSQL_DEFAULT_HANDLE)
  1361. {
  1362. new Cache:result, count;
  1363. format(SQL::upd_form, sizeof(SQL::upd_form), "SELECT `%s` FROM `%s` WHERE `%s`%s'%d' ", field, table, column_where, column_whereo, row_identifier);
  1364. result = mysql_query(connectionHandle, SQL::upd_form);
  1365. if(mysql_errno() != 0)
  1366. {
  1367. return SQL_Warning("Query could not be completed due to error: %s", SQL::upd_form);
  1368. }
  1369. if(result)
  1370. {
  1371. cache_get_row_count(count);
  1372. if(count == 1)
  1373. {
  1374. cache_get_value_name(0, field, SQL::upd_form, sizeof(SQL::upd_form));
  1375. strcpy(dest, SQL::upd_form, len);
  1376. cache_delete(result);
  1377. }
  1378. else
  1379. {
  1380. cache_delete(result);
  1381. return 0;
  1382. }
  1383. }
  1384. return 1;
  1385. }
  1386. stock SQL::GetStringEntry2(const table[], const field[], const column_where[] = "", row_identifier = -1, const column_where2[] = "", row_identifier2 = -1, const row_identifier3[] = "", dest[], len = sizeof(dest), const column_whereo[] = "=", const column_whereo2[] = "=", MySQL:connectionHandle = MYSQL_DEFAULT_HANDLE)
  1387. {
  1388. new Cache:result, count;
  1389. format(SQL::upd_form, sizeof(SQL::upd_form), "SELECT `%s` FROM `%s` WHERE `%s`%s'%d'", field, table, column_where, column_whereo, row_identifier);
  1390. if(!isnull(column_where2) && row_identifier2 != -1 && isnull(row_identifier3))
  1391. {
  1392. format(SQL::upd_form2, sizeof(SQL::upd_form2), " AND `%s`%s'%d'", column_where2, column_whereo2, row_identifier2);
  1393. strcat(SQL::upd_form, SQL::upd_form2);
  1394. }
  1395. if(!isnull(column_where2) && !isnull(row_identifier3) && row_identifier2 == -1)
  1396. {
  1397. format(SQL::upd_form2, sizeof(SQL::upd_form2), " AND `%s`%s'%s'", column_where2, column_whereo2, row_identifier3);
  1398. strcat(SQL::upd_form, SQL::upd_form2);
  1399. }
  1400. strcat(SQL::upd_form, " ");
  1401. result = mysql_query(connectionHandle, SQL::upd_form);
  1402. if(mysql_errno() != 0)
  1403. {
  1404. return SQL_Warning("Query could not be completed due to error: %s", SQL::upd_form);
  1405. }
  1406. if(result)
  1407. {
  1408. cache_get_row_count(count);
  1409. if(count == 1)
  1410. {
  1411. cache_get_value_name(0, field, SQL::upd_form, sizeof(SQL::upd_form));
  1412. strcpy(dest, SQL::upd_form, len);
  1413. cache_delete(result);
  1414. }
  1415. else
  1416. {
  1417. cache_delete(result);
  1418. return 0;
  1419. }
  1420. }
  1421. return 1;
  1422. }
  1423. stock SQL::GetStringEntryEx(const table[], const field[], const column_where[] = "", const scolumn_where[] = "", dest[], len = sizeof(dest), const column_whereo[] = "=", MySQL:connectionHandle = MYSQL_DEFAULT_HANDLE)
  1424. {
  1425. new Cache:result, count;
  1426. mysql_format(connectionHandle, SQL::upd_form, sizeof(SQL::upd_form), "SELECT `%s` FROM `%s` WHERE `%s`%s'%e' ", field, table, column_where, column_whereo, scolumn_where);
  1427. result = mysql_query(connectionHandle, SQL::upd_form);
  1428. if(mysql_errno() != 0)
  1429. {
  1430. return SQL_Warning("Query could not be completed due to error: %s", SQL::upd_form);
  1431. }
  1432. if(result)
  1433. {
  1434. cache_get_row_count(count);
  1435. if(count == 1)
  1436. {
  1437. cache_get_value_name(0, field, SQL::upd_form, sizeof(SQL::upd_form));
  1438. strcpy(dest, SQL::upd_form, len);
  1439. cache_delete(result);
  1440. }
  1441. else
  1442. {
  1443. cache_delete(result);
  1444. return 0;
  1445. }
  1446. }
  1447. return 1;
  1448. }
  1449. stock SQL::GetStringEntryEx2(const table[], const field[], const column_where[] = "", const scolumn_where[] = "", const column_where2[] = "", const scolumn_where2[] = "", scolumn_where3 = -1, dest[], len = sizeof(dest), const column_whereo[] = "=", const column_whereo2[] = "=", MySQL:connectionHandle = MYSQL_DEFAULT_HANDLE)
  1450. {
  1451. new Cache:result, count;
  1452. mysql_format(connectionHandle, SQL::upd_form, sizeof(SQL::upd_form), "SELECT `%s` FROM `%s` WHERE `%s`%s'%e'", field, table, column_where, column_whereo, scolumn_where);
  1453. if(!isnull(column_where2) && !isnull(scolumn_where2) && scolumn_where3 == -1)
  1454. {
  1455. format(SQL::upd_form2, sizeof(SQL::upd_form2), " AND `%s`%s'%e'", column_where2, column_whereo2, scolumn_where2);
  1456. strcat(SQL::upd_form, SQL::upd_form2);
  1457. }
  1458. if(!isnull(column_where2) && scolumn_where3 != -1 && isnull(scolumn_where2))
  1459. {
  1460. format(SQL::upd_form2, sizeof(SQL::upd_form2), " AND `%s`%s'%d'", column_where2, column_whereo2, scolumn_where3);
  1461. strcat(SQL::upd_form, SQL::upd_form2);
  1462. }
  1463. strcat(SQL::upd_form, " ");
  1464. result = mysql_query(connectionHandle, SQL::upd_form);
  1465. if(mysql_errno() != 0)
  1466. {
  1467. return SQL_Warning("Query could not be completed due to error: %s", SQL::upd_form);
  1468. }
  1469. if(result)
  1470. {
  1471. cache_get_row_count(count);
  1472. if(count == 1)
  1473. {
  1474. cache_get_value_name(0, field, SQL::upd_form, sizeof(SQL::upd_form));
  1475. strcpy(dest, SQL::upd_form, len);
  1476. cache_delete(result);
  1477. }
  1478. else
  1479. {
  1480. cache_delete(result);
  1481. return 0;
  1482. }
  1483. }
  1484. return 1;
  1485. }
  1486. stock SQL::t_GetStringEntryEx(const table[], const field[], const column_where[] = "", const scolumn_where[] = "", const column_whereo[] = "=", MySQL:connectionHandle = MYSQL_DEFAULT_HANDLE)
  1487. {
  1488. new Cache:result, count;
  1489. mysql_format(connectionHandle, SQL::upd_form, sizeof(SQL::upd_form), "SELECT `%s` FROM `%s` WHERE `%s`%s'%e' ", field, table, column_where, column_whereo, scolumn_where);
  1490. result = mysql_query(connectionHandle, SQL::upd_form);
  1491. if(mysql_errno() != 0)
  1492. {
  1493. return SQL_Warning("Query could not be completed due to error: %s", SQL::upd_form);
  1494. }
  1495. SQL::upd_form[0] = EOS;
  1496. if(result)
  1497. {
  1498. cache_get_row_count(count);
  1499. if(count == 1)
  1500. {
  1501. cache_get_value_name(0, field, SQL::upd_form, sizeof(SQL::upd_form));
  1502. cache_delete(result);
  1503. }
  1504. else
  1505. {
  1506. cache_delete(result);
  1507. return SQL::upd_form,;
  1508. }
  1509. }
  1510. return SQL::upd_form,;
  1511. }
  1512. stock SQL::t_GetStringEntryEx2(const table[], const field[], const column_where[] = "", const scolumn_where[] = "", const column_where2[] = "", const scolumn_where2[] = "", scolumn_where3 = -1, const column_whereo[] = "=", const column_whereo2[] = "=", MySQL:connectionHandle = MYSQL_DEFAULT_HANDLE)
  1513. {
  1514. new Cache:result, count;
  1515. mysql_format(connectionHandle, SQL::upd_form, sizeof(SQL::upd_form), "SELECT `%s` FROM `%s` WHERE `%s`%s'%e'", field, table, column_where, column_whereo, scolumn_where);
  1516. if(!isnull(column_where2) && !isnull(scolumn_where2) && scolumn_where3 == -1)
  1517. {
  1518. format(SQL::upd_form2, sizeof(SQL::upd_form2), " AND `%s`%s'%e'", column_where2, column_whereo2, scolumn_where2);
  1519. strcat(SQL::upd_form, SQL::upd_form2);
  1520. }
  1521. if(!isnull(column_where2) && scolumn_where3 != -1 && isnull(scolumn_where2))
  1522. {
  1523. format(SQL::upd_form2, sizeof(SQL::upd_form2), " AND `%s`%s'%d'", column_where2, column_whereo2, scolumn_where3);
  1524. strcat(SQL::upd_form, SQL::upd_form2);
  1525. }
  1526. strcat(SQL::upd_form, " ");
  1527. result = mysql_query(connectionHandle, SQL::upd_form);
  1528. if(mysql_errno() != 0)
  1529. {
  1530. return SQL_Warning("Query could not be completed due to error: %s", SQL::upd_form);
  1531. }
  1532. SQL::upd_form[0] = EOS;
  1533. if(result)
  1534. {
  1535. cache_get_row_count(count);
  1536. if(count == 1)
  1537. {
  1538. cache_get_value_name(0, field, SQL::upd_form, sizeof(SQL::upd_form));
  1539. cache_delete(result);
  1540. }
  1541. else
  1542. {
  1543. cache_delete(result);
  1544. return SQL::upd_form,;
  1545. }
  1546. }
  1547. return SQL::upd_form,;
  1548. }
  1549. stock SQL::GetIntEntryEx(const table[], const field[], const column_where[] = "", scolumn_where[] = "", const column_whereo[] = "=", MySQL:connectionHandle = MYSQL_DEFAULT_HANDLE)
  1550. {
  1551. new Cache:result, int, count;
  1552. mysql_format(connectionHandle, SQL::upd_form, sizeof(SQL::upd_form), "SELECT `%s` FROM `%s` WHERE `%s`%s'%e' ", field, table, column_where, column_whereo, scolumn_where);
  1553. result = mysql_query(connectionHandle, SQL::upd_form);
  1554. if(mysql_errno() != 0)
  1555. {
  1556. return SQL_Warning("Query could not be completed due to error: %s", SQL::upd_form);
  1557. }
  1558. if(result)
  1559. {
  1560. cache_get_row_count(count);
  1561. if(count == 1)
  1562. {
  1563. cache_get_value_name_int(0, field, int);
  1564. }
  1565. else
  1566. {
  1567. int = -1;
  1568. }
  1569. cache_delete(result);
  1570. }
  1571. return int;
  1572. }
  1573. stock SQL::GetIntEntryEx2(const table[], const field[], const column_where[] = "", scolumn_where[] = "", const column_where2[] = "", scolumn_where2[] = "", scolumn_where3 = -1, const column_whereo[] = "=", const column_whereo2[] = "=", MySQL:connectionHandle = MYSQL_DEFAULT_HANDLE)
  1574. {
  1575. new Cache:result, int, count;
  1576. mysql_format(connectionHandle, SQL::upd_form, sizeof(SQL::upd_form), "SELECT `%s` FROM `%s` WHERE `%s`%s'%e'", field, table, column_where, column_whereo, scolumn_where);
  1577. if(!isnull(column_where2) && !isnull(scolumn_where2) && scolumn_where3 == -1)
  1578. {
  1579. format(SQL::upd_form2, sizeof(SQL::upd_form2), " AND `%s`%s'%e'", column_where2, column_whereo2, scolumn_where2);
  1580. strcat(SQL::upd_form, SQL::upd_form2);
  1581. }
  1582. if(!isnull(column_where2) && scolumn_where3 != -1 && isnull(scolumn_where2))
  1583. {
  1584. format(SQL::upd_form2, sizeof(SQL::upd_form2), " AND `%s`%s'%d'", column_where2, column_whereo2, scolumn_where3);
  1585. strcat(SQL::upd_form, SQL::upd_form2);
  1586. }
  1587. strcat(SQL::upd_form, " ");
  1588. result = mysql_query(connectionHandle, SQL::upd_form);
  1589. if(mysql_errno() != 0)
  1590. {
  1591. return SQL_Warning("Query could not be completed due to error: %s", SQL::upd_form);
  1592. }
  1593. if(result)
  1594. {
  1595. cache_get_row_count(count);
  1596. if(count == 1)
  1597. {
  1598. cache_get_value_name_int(0, field, int);
  1599. }
  1600. else
  1601. {
  1602. int = -1;
  1603. }
  1604. cache_delete(result);
  1605. }
  1606. return int;
  1607. }
  1608. stock Float:SQL::GetFloatEntryEx(const table[], const field[], const column_where[] = "", scolumn_where[] = "", const column_whereo[] = "=", MySQL:connectionHandle = MYSQL_DEFAULT_HANDLE)
  1609. {
  1610. new Cache:result, Float:int, count;
  1611. mysql_format(connectionHandle, SQL::upd_form, sizeof(SQL::upd_form), "SELECT `%s` FROM `%s` WHERE `%s`%s'%e' ", field, table, column_where, column_whereo, scolumn_where);
  1612. result = mysql_query(connectionHandle, SQL::upd_form);
  1613. if(mysql_errno() != 0)
  1614. {
  1615. return SQL_Warning("Query could not be completed due to error: %s", SQL::upd_form);
  1616. }
  1617. if(result)
  1618. {
  1619. cache_get_row_count(count);
  1620. if(count == 1)
  1621. {
  1622. cache_get_value_name_float(0, field, int);
  1623. }
  1624. else
  1625. {
  1626. int = -1.0;
  1627. }
  1628. cache_delete(result);
  1629. }
  1630. return int;
  1631. }
  1632. stock Float:SQL::GetFloatEntryEx2(const table[], const field[], const column_where[] = "", scolumn_where[] = "", const column_where2[] = "", scolumn_where2[] = "", scolumn_where3 = -1, const column_whereo[] = "=", const column_whereo2[] = "=", MySQL:connectionHandle = MYSQL_DEFAULT_HANDLE)
  1633. {
  1634. new Cache:result, Float:int, count;
  1635. mysql_format(connectionHandle, SQL::upd_form, sizeof(SQL::upd_form), "SELECT `%s` FROM `%s` WHERE `%s`%s'%e'", field, table, column_where, column_whereo, scolumn_where);
  1636. if(!isnull(column_where2) && !isnull(scolumn_where2) && scolumn_where3 == -1)
  1637. {
  1638. format(SQL::upd_form2, sizeof(SQL::upd_form2), " AND `%s`%s'%e'", column_where2, column_whereo2, scolumn_where2);
  1639. strcat(SQL::upd_form, SQL::upd_form2);
  1640. }
  1641. if(!isnull(column_where2) && scolumn_where3 != -1 && isnull(scolumn_where2))
  1642. {
  1643. format(SQL::upd_form2, sizeof(SQL::upd_form2), " AND `%s`%s'%d'", column_where2, column_whereo2, scolumn_where3);
  1644. strcat(SQL::upd_form, SQL::upd_form2);
  1645. }
  1646. strcat(SQL::upd_form, " ");
  1647. result = mysql_query(connectionHandle, SQL::upd_form);
  1648. if(mysql_errno() != 0)
  1649. {
  1650. return SQL_Warning("Query could not be completed due to error: %s", SQL::upd_form);
  1651. }
  1652. if(result)
  1653. {
  1654. cache_get_row_count(count);
  1655. if(count == 1)
  1656. {
  1657. cache_get_value_name_int(0, field, int);
  1658. }
  1659. else
  1660. {
  1661. int = -1.0;
  1662. }
  1663. cache_delete(result);
  1664. }
  1665. return int;
  1666. }
  1667. stock SQL::AddTableColumn(handle, const field_name[], SQL::datatypes: type = SQL_TYPE_INT, maxlength = 11, bool:auto_increment = false, bool:setprimary = false, bool:setindex = false)
  1668. {
  1669. if(!SQL::IsValidUpdatingSlot(handle))
  1670. {
  1671. SQL_Error("(SQL::AddTableColumn) Invalid handle. Make sure you used (SQL::Open) first.");
  1672. return 0;
  1673. }
  1674. if(SQL::upd_type[handle] != SQL::CREATE) return 0;
  1675. if(SQL::isset_primarykey[handle] && setprimary == true)
  1676. {
  1677. SQL_Error("(SQL::AddTableColumn) A primary column_where was created already. There can only be one primary column_where! [Key %s]", field_name);
  1678. return 0;
  1679. }
  1680. if(auto_increment == true && type == SQL_TYPE_VCHAR)
  1681. {
  1682. SQL_Error("(SQL::AddTableColumn) You can't set auto increment if the type is varchar! [Key %s]", field_name);
  1683. return 0;
  1684. }
  1685. switch(type)
  1686. {
  1687. case SQL_TYPE_INT:
  1688. {
  1689. if(auto_increment == true && setprimary == false)
  1690. {
  1691. format(SQL::upd_form, sizeof(SQL::upd_form), "%s int NOT NULL AUTO_INCREMENT,", field_name, maxlength);
  1692. strcat(SQL::upd_query[handle], SQL::upd_form);
  1693. SQL::upd_datacount[handle]++;
  1694. SQL::isset_primarykey[handle] = 1;
  1695. strcpy(SQL::primarykey[handle], field_name);
  1696. }
  1697. else if(auto_increment == true && setprimary == true)
  1698. {
  1699. format(SQL::upd_form, sizeof(SQL::upd_form), "%s int NOT NULL AUTO_INCREMENT,", field_name, maxlength);
  1700. strcat(SQL::upd_query[handle], SQL::upd_form);
  1701. SQL::upd_datacount[handle]++;
  1702. SQL::isset_primarykey[handle] = 1;
  1703. strcpy(SQL::primarykey[handle], field_name);
  1704. }
  1705. else if(setprimary == true)
  1706. {
  1707. format(SQL::upd_form, sizeof(SQL::upd_form), "%s int NOT NULL PRIMARY KEY,", field_name, maxlength);
  1708. strcat(SQL::upd_query[handle], SQL::upd_form);
  1709. SQL::upd_datacount[handle]++;
  1710. SQL::isset_primarykey[handle] = 2;
  1711. strcpy(SQL::primarykey[handle], field_name);
  1712. }
  1713. else
  1714. {
  1715. format(SQL::upd_form, sizeof(SQL::upd_form), "%s int(%d) NOT NULL,", field_name, maxlength);
  1716. strcat(SQL::upd_query[handle], SQL::upd_form);
  1717. SQL::upd_datacount[handle]++;
  1718. }
  1719. }
  1720. case SQL_TYPE_VCHAR:
  1721. {
  1722. if(auto_increment == true) return 0;
  1723. if(setprimary == true)
  1724. {
  1725. format(SQL::upd_form, sizeof(SQL::upd_form), "%s varchar(%d) NOT NULL PRIMARY KEY,", field_name, maxlength);
  1726. strcat(SQL::upd_query[handle], SQL::upd_form);
  1727. SQL::upd_datacount[handle]++;
  1728. SQL::isset_primarykey[handle] = 2;
  1729. strcpy(SQL::primarykey[handle], field_name);
  1730. }
  1731. else
  1732. {
  1733. format(SQL::upd_form, sizeof(SQL::upd_form), "%s varchar(%d) NOT NULL,", field_name, maxlength);
  1734. strcat(SQL::upd_query[handle], SQL::upd_form);
  1735. SQL::upd_datacount[handle]++;
  1736. }
  1737. }
  1738. case SQL_TYPE_FLOAT:
  1739. {
  1740. if(auto_increment == true) return 0;
  1741. if(setprimary == true)
  1742. {
  1743. format(SQL::upd_form, sizeof(SQL::upd_form), "%s float(%d) NOT NULL PRIMARY KEY,", field_name, maxlength);
  1744. strcat(SQL::upd_query[handle], SQL::upd_form);
  1745. SQL::upd_datacount[handle]++;
  1746. SQL::isset_primarykey[handle] = 2;
  1747. strcpy(SQL::primarykey[handle], field_name);
  1748. }
  1749. else
  1750. {
  1751. format(SQL::upd_form, sizeof(SQL::upd_form), "%s float(%d) NOT NULL,", field_name, maxlength);
  1752. strcat(SQL::upd_query[handle], SQL::upd_form);
  1753. SQL::upd_datacount[handle]++;
  1754. }
  1755. }
  1756. }
  1757. if(setindex == true)
  1758. {
  1759. new indexslot = SQL::GetFreeIndexSlot(handle);
  1760. if(indexslot == SQL_INVALID_HANDLE) return SQL_Error("(SQL::AddTableColumn) Indexes limit reached. Increase the limit in MAX_INDEXES");
  1761. SQL::index_set[handle][indexslot] = 1;
  1762. strcpy(SQL::index[handle][indexslot], field_name);
  1763. }
  1764. return 1;
  1765. }
  1766. stock SQL::ReadInt(handle, const field[], &dest, row = 0)
  1767. {
  1768. if(!SQL::IsValidUpdatingSlot(handle))
  1769. {
  1770. SQL_Error("(SQL::ReadInt) Invalid handle. Make sure you opened the table first.");
  1771. return 0;
  1772. }
  1773. if(SQL::upd_type[handle] == SQL::READ)
  1774. {
  1775. cache_get_value_name_int(0, field, dest);
  1776. SQL::upd_datacount[handle]++;
  1777. }
  1778. else if(SQL::upd_type[handle] == SQL::CALLBACK)
  1779. {
  1780. cache_get_value_name_int(row, field, dest);
  1781. SQL::upd_datacount[handle]++;
  1782. }
  1783. else return 0;
  1784. return 1;
  1785. }
  1786. stock SQL::ReadFloat(handle, const field[], &Float:dest, row = 0)
  1787. {
  1788. if(!SQL::IsValidUpdatingSlot(handle))
  1789. {
  1790. SQL_Error("(SQL::ReadInt) Invalid handle. Make sure you opened the table first.");
  1791. return 0;
  1792. }
  1793. if(SQL::upd_type[handle] == SQL::READ)
  1794. {
  1795. cache_get_value_name_float(0, field, dest);
  1796. SQL::upd_datacount[handle]++;
  1797. }
  1798. else if(SQL::upd_type[handle] == SQL::CALLBACK)
  1799. {
  1800. cache_get_value_name_float(row, field, dest);
  1801. SQL::upd_datacount[handle]++;
  1802. }
  1803. else return 0;
  1804. return 1;
  1805. }
  1806. stock SQL::ReadString(handle, const field[], dest[], len = sizeof(dest), row = 0)
  1807. {
  1808. if(!SQL::IsValidUpdatingSlot(handle))
  1809. {
  1810. SQL_Error("(SQL::ReadInt) Invalid handle. Make sure you opened the table first.");
  1811. return 0;
  1812. }
  1813. if(SQL::upd_type[handle] == SQL::READ)
  1814. {
  1815. cache_get_value_name(0, field, dest, len+1);
  1816. SQL::upd_datacount[handle]++;
  1817. }
  1818. else if(SQL::upd_type[handle] == SQL::CALLBACK)
  1819. {
  1820. cache_get_value_name(row, field, dest, len+1);
  1821. SQL::upd_datacount[handle]++;
  1822. }
  1823. else return 0;
  1824. return 1;
  1825. }
  1826. stock SQL::UpdateIntEntry(handle, const field[], value)
  1827. {
  1828. if(!SQL::IsValidUpdatingSlot(handle))
  1829. {
  1830. SQL_Error("(SQL::UpdateIntEntry) Invalid handle. Make sure you used (SQL::OpenTable) first.");
  1831. return 0;
  1832. }
  1833. if(SQL::upd_type[handle] != SQL::UPDATE && SQL::upd_type[handle] != SQL::TUPDATE) return 0;
  1834. format(SQL::upd_form, sizeof(SQL::upd_form), "`%s`='%i',", field, value);
  1835. strcat(SQL::upd_query[handle], SQL::upd_form);
  1836. SQL::upd_datacount[handle]++;
  1837. return 1;
  1838. }
  1839. stock SQL::WriteInt(handle, const field[], value)
  1840. {
  1841. if(!SQL::IsValidUpdatingSlot(handle))
  1842. {
  1843. SQL_Error("(SQL::WriteInt) Invalid handle. Make sure you used (SQL::Open) first.");
  1844. return 0;
  1845. }
  1846. switch(SQL::upd_type[handle])
  1847. {
  1848. case SQL::UPDATE:
  1849. {
  1850. return SQL::UpdateIntEntry(handle, field, value);
  1851. }
  1852. case SQL::TUPDATE:
  1853. {
  1854. return SQL::UpdateIntEntry(handle, field, value);
  1855. }
  1856. case SQL::INSERT:
  1857. {
  1858. return SQL::InsertIntEntry(handle, field, value);
  1859. }
  1860. }
  1861. return 0;
  1862. }
  1863. stock SQL::WriteFloat(handle, const field[], Float:value)
  1864. {
  1865. if(!SQL::IsValidUpdatingSlot(handle))
  1866. {
  1867. SQL_Error("(SQL::WriteFloat) Invalid handle. Make sure you used (SQL::Open) first.");
  1868. return 0;
  1869. }
  1870. switch(SQL::upd_type[handle])
  1871. {
  1872. case SQL::UPDATE:
  1873. {
  1874. return SQL::UpdateFloatEntry(handle, field, value);
  1875. }
  1876. case SQL::TUPDATE:
  1877. {
  1878. return SQL::UpdateFloatEntry(handle, field, value);
  1879. }
  1880. case SQL::INSERT:
  1881. {
  1882. return SQL::InsertFloatEntry(handle, field, value);
  1883. }
  1884. }
  1885. return 0;
  1886. }
  1887. stock SQL::WriteString(handle, const field[], const value[], bool: use_real_escape = true)
  1888. {
  1889. if(!SQL::IsValidUpdatingSlot(handle))
  1890. {
  1891. SQL_Error("(SQL::WriteFloat) Invalid handle. Make sure you used (SQL::Open) first.");
  1892. return 0;
  1893. }
  1894. switch(SQL::upd_type[handle])
  1895. {
  1896. case SQL::UPDATE:
  1897. {
  1898. return SQL::UpdateStringEntry(handle, field, value, use_real_escape);
  1899. }
  1900. case SQL::TUPDATE:
  1901. {
  1902. return SQL::UpdateStringEntry(handle, field, value, use_real_escape);
  1903. }
  1904. case SQL::INSERT:
  1905. {
  1906. return SQL::InsertStringEntry(handle, field, value, use_real_escape);
  1907. }
  1908. }
  1909. return 0;
  1910. }
  1911. stock SQL::Close(handle)
  1912. {
  1913. if(!SQL::IsValidUpdatingSlot(handle))
  1914. {
  1915. SQL_Error("(SQL::Close) Invalid handle. Make sure you opened a handle first.");
  1916. return 0;
  1917. }
  1918. new MySQL:connectionHandle = SQL::upd_connectionHandle[handle];
  1919. if(SQL::upd_type[handle] == SQL::UPDATE)
  1920. {
  1921. if(!SQL::upd_datacount[handle])
  1922. {
  1923. SQL_Error("%s", SQL::upd_query[handle]);
  1924. SQL_Error("(SQL::Close) No data has been updated.");
  1925. SQL::UsedHandle[handle] = false;
  1926. return 0;
  1927. }
  1928. //Remove last comma
  1929. new pos = strlen(SQL::upd_query[handle])-1;
  1930. SQL::upd_query[handle][pos] = ' ';
  1931. strcat(SQL::upd_query[handle], SQL::upd_rowidentifier[handle]);
  1932. mysql_tquery(connectionHandle, SQL::upd_query[handle], "", "");
  1933. SQL::upd_table[handle][0] = '\0';
  1934. SQL::upd_query[handle][0] = '\0';
  1935. SQL::upd_rowidentifier[handle][0] = '\0';
  1936. SQL::upd_datacount[handle] = 0;
  1937. SQL::UsedHandle[handle] = false;
  1938. }
  1939. else if(SQL::upd_type[handle] == SQL::TUPDATE)
  1940. {
  1941. //Remove last comma
  1942. new pos = strlen(SQL::upd_query[handle])-1;
  1943. SQL::upd_query[handle][pos] = ' ';
  1944. mysql_tquery(connectionHandle, SQL::upd_query[handle], "", "");
  1945. SQL::upd_table[handle][0] = '\0';
  1946. SQL::upd_query[handle][0] = '\0';
  1947. SQL::upd_datacount[handle] = 0;
  1948. SQL::UsedHandle[handle] = false;
  1949. }
  1950. else if(SQL::upd_type[handle] == SQL::CREATE)
  1951. {
  1952. if(!SQL::upd_datacount[handle])
  1953. {
  1954. SQL_Error("%s", SQL::upd_query[handle]);
  1955. SQL_Error("(SQL::Close) No data has been added..");
  1956. SQL::UsedHandle[handle] = false;
  1957. return 0;
  1958. }
  1959. new pos = strlen(SQL::upd_query[handle])-1;
  1960. SQL::upd_query[handle][pos] = ' ';
  1961. for(new i = 0; i != SQL_MAX_INDEXES; i++)
  1962. {
  1963. if(SQL::index_set[handle][i])
  1964. {
  1965. format(SQL::upd_form, 128, ",INDEX (`%s`)", SQL::index[handle][i]);
  1966. strcat(SQL::upd_query[handle], SQL::upd_form);
  1967. SQL::index_set[handle][i] = 0;
  1968. SQL::index[handle][i][0] = EOS;
  1969. }
  1970. }
  1971. if(SQL::isset_primarykey[handle] == 1)
  1972. {
  1973. format(SQL::upd_form, 128, ",primary key (%s)", SQL::primarykey[handle]);
  1974. strcat(SQL::upd_query[handle], SQL::upd_form);
  1975. }
  1976. strcat(SQL::upd_query[handle], ");");
  1977. mysql_tquery(connectionHandle, SQL::upd_query[handle], "", "");
  1978. SQL::upd_table[handle][0] = '\0';
  1979. //SQL::upd_connectionHandle[handle] = MYSQL_INVALID_HANDLE;
  1980. SQL::primarykey[handle][0] = '\0';
  1981. SQL::isset_primarykey[handle] = false;
  1982. SQL::upd_query[handle][0] = '\0';
  1983. SQL::upd_rowidentifier[handle][0] = '\0';
  1984. SQL::upd_datacount[handle] = 0;
  1985. SQL::UsedHandle[handle] = false;
  1986. }
  1987. else if(SQL::upd_type[handle] == SQL::INSERT)
  1988. {
  1989. if(!SQL::upd_datacount[handle])
  1990. {
  1991. SQL_Error("%s", SQL::upd_query[handle]);
  1992. SQL_Error("(SQL::Close) No data has been inserted..");
  1993. SQL::UsedHandle[handle] = false;
  1994. return 0;
  1995. }
  1996. new pos = strlen(SQL::upd_query[handle])-1;
  1997. SQL::upd_query[handle][pos] = ')';
  1998. pos = strlen(SQL::upd_query_2[handle])-1;
  1999. SQL::upd_query_2[handle][pos] = ' ';
  2000. strcat(SQL::upd_query_2[handle], ");");
  2001. strcat(SQL::upd_query[handle], SQL::upd_query_2[handle]);
  2002. SQL::upd_table[handle][0] = '\0';
  2003. SQL::upd_query_2[handle][0] = '\0';
  2004. SQL::upd_rowidentifier[handle][0] = '\0';
  2005. SQL::upd_datacount[handle] = 0;
  2006. if(SQL::upd_useautoincrement[handle] == true)
  2007. {
  2008. new Cache:result;
  2009. result = mysql_query(connectionHandle, SQL::upd_query[handle]);
  2010. if(mysql_errno() != 0)
  2011. {
  2012. return SQL_Warning("Query could not be completed due to error: %s", SQL::upd_query[handle]);
  2013. }
  2014. if(result)
  2015. {
  2016. new id = cache_insert_id();
  2017. cache_delete(result);
  2018. SQL::upd_query[handle][0] = '\0';
  2019. SQL::UsedHandle[handle] = false;
  2020. return id;
  2021. }
  2022. }
  2023. else
  2024. {
  2025. mysql_tquery(connectionHandle, SQL::upd_query[handle], "", "");
  2026. SQL::UsedHandle[handle] = false;
  2027. }
  2028. }
  2029. else if(SQL::upd_type[handle] == SQL::READ)
  2030. {
  2031. if(!SQL::upd_datacount[handle])
  2032. {
  2033. SQL_Error("%s", SQL::upd_query[handle]);
  2034. SQL_Error("(SQL::Close) No data has been read..");
  2035. SQL::UsedHandle[handle] = false;
  2036. return 0;
  2037. }
  2038. if(cache_is_valid(SQL::ReadCache[handle]))
  2039. {
  2040. cache_delete(SQL::ReadCache[handle]);
  2041. }
  2042. SQL::UsedHandle[handle] = false;
  2043. }
  2044. else if(SQL::upd_type[handle] == SQL::CALLBACK)
  2045. {
  2046. new count;
  2047. cache_get_row_count(count);
  2048. if(cache_is_valid(SQL::ReadCache[handle]))
  2049. {
  2050. cache_delete(SQL::ReadCache[handle]);
  2051. }
  2052. SQL::UsedHandle[handle] = false;
  2053. return count;
  2054. }
  2055. return -1;
  2056. }
  2057. stock SQL::SetIntEntry(const table[], const field[], value, const column_where[] = "", row_identifier = -1, MySQL:connectionHandle = MYSQL_DEFAULT_HANDLE)
  2058. {
  2059. if(!table[0]) return 0;
  2060. if(!field[0]) return 0;
  2061. format(SQL::upd_form, sizeof(SQL::upd_form), "UPDATE `%s` SET `%s`='%d' WHERE `%s`='%d' ", table, field, value, column_where, row_identifier);
  2062. mysql_tquery(connectionHandle, SQL::upd_form, "", "");
  2063. return 1;
  2064. }
  2065. stock SQL::SetIntEntry2(const table[], const field[], value, const column_where[] = "", row_identifier = -1, const column_where2[] = "", row_identifier2 = -1, const row_identifier3[] = "", MySQL:connectionHandle = MYSQL_DEFAULT_HANDLE)
  2066. {
  2067. if(!table[0]) return 0;
  2068. if(!field[0]) return 0;
  2069. format(SQL::upd_form, sizeof(SQL::upd_form), "UPDATE `%s` SET `%s`='%d' WHERE `%s`='%d'", table, field, value, column_where, row_identifier);
  2070. if(!isnull(column_where2) && row_identifier2 != -1 && isnull(row_identifier3))
  2071. {
  2072. format(SQL::upd_form2, sizeof(SQL::upd_form2), " AND `%s`='%d'", column_where2, row_identifier2);
  2073. strcat(SQL::upd_form, SQL::upd_form2);
  2074. }
  2075. if(!isnull(column_where2) && !isnull(row_identifier3) && row_identifier2 == -1)
  2076. {
  2077. format(SQL::upd_form2, sizeof(SQL::upd_form2), " AND `%s`='%s'", column_where2, row_identifier3);
  2078. strcat(SQL::upd_form, SQL::upd_form2);
  2079. }
  2080. strcat(SQL::upd_form, " ");
  2081. mysql_tquery(connectionHandle, SQL::upd_form, "", "");
  2082. return 1;
  2083. }
  2084. stock SQL::SetIntEntryEx(const table[], const field[], value, const column_where[] = "", const row_identifier[] = "", MySQL:connectionHandle = MYSQL_DEFAULT_HANDLE)
  2085. {
  2086. if(!table[0]) return 0;
  2087. if(!field[0]) return 0;
  2088. mysql_format(connectionHandle, SQL::upd_form, sizeof(SQL::upd_form), "UPDATE `%s` SET `%s`='%d' WHERE `%s`='%e' ", table, field, value, column_where, row_identifier);
  2089. mysql_tquery(connectionHandle, SQL::upd_form, "", "");
  2090. return 1;
  2091. }
  2092. stock SQL::SetIntEntryEx2(const table[], const field[], value, const column_where[] = "", const row_identifier[] = "", const column_where2[] = "", const row_identifier2[] = "", row_identifier3 = -1, MySQL:connectionHandle = MYSQL_DEFAULT_HANDLE)
  2093. {
  2094. if(!table[0]) return 0;
  2095. if(!field[0]) return 0;
  2096. mysql_format(connectionHandle, SQL::upd_form, sizeof(SQL::upd_form), "UPDATE `%s` SET `%s`='%d' WHERE `%s`='%e'", table, field, value, column_where, row_identifier);
  2097. if(!isnull(column_where2) && !isnull(row_identifier2) && row_identifier3 == -1)
  2098. {
  2099. format(SQL::upd_form2, sizeof(SQL::upd_form2), " AND `%s`='%e'", column_where2, row_identifier2);
  2100. strcat(SQL::upd_form, SQL::upd_form2);
  2101. }
  2102. if(!isnull(column_where2) && row_identifier3 != -1 && isnull(row_identifier2))
  2103. {
  2104. format(SQL::upd_form2, sizeof(SQL::upd_form2), " AND `%s`='%d'", column_where2, row_identifier3);
  2105. strcat(SQL::upd_form, SQL::upd_form2);
  2106. }
  2107. strcat(SQL::upd_form, " ");
  2108. mysql_tquery(connectionHandle, SQL::upd_form, "", "");
  2109. return 1;
  2110. }
  2111. stock SQL::SetStringEntry(const table[], const field[], const value[], const column_where[] = "", row_identifier = -1, bool:use_real_escape = true, MySQL:connectionHandle = MYSQL_DEFAULT_HANDLE)
  2112. {
  2113. if(!table[0]) return 0;
  2114. if(!field[0]) return 0;
  2115. if(use_real_escape == true)
  2116. {
  2117. //Not using mysql_escape_string as the %e specifier is faster.
  2118. mysql_format(connectionHandle, SQL::upd_form, sizeof(SQL::upd_form), "UPDATE `%s` SET `%s`='%e' WHERE `%s`='%d' ", table, field, value, column_where, row_identifier);
  2119. }
  2120. else
  2121. {
  2122. format(SQL::upd_form, sizeof(SQL::upd_form), "UPDATE `%s` SET `%s`='%s' WHERE `%s`='%d' ", table, field, value, column_where, row_identifier);
  2123. }
  2124. mysql_tquery(connectionHandle, SQL::upd_form, "", "");
  2125. return 1;
  2126. }
  2127. stock SQL::SetStringEntry2(const table[], const field[], const value[], const column_where[] = "", row_identifier = -1, const column_where2[] = "", row_identifier2 = -1, const row_identifier3[] = "", bool:use_real_escape = true, MySQL:connectionHandle = MYSQL_DEFAULT_HANDLE)
  2128. {
  2129. if(!table[0]) return 0;
  2130. if(!field[0]) return 0;
  2131. if(use_real_escape == true)
  2132. {
  2133. //Not using mysql_escape_string as the %e specifier is faster.
  2134. mysql_format(connectionHandle, SQL::upd_form, sizeof(SQL::upd_form), "UPDATE `%s` SET `%s`='%e' WHERE `%s`='%d'", table, field, value, column_where, row_identifier);
  2135. }
  2136. else
  2137. {
  2138. format(SQL::upd_form, sizeof(SQL::upd_form), "UPDATE `%s` SET `%s`='%e' WHERE `%s`='%d'", table, field, value, column_where, row_identifier);
  2139. }
  2140. if(!isnull(column_where2) && row_identifier2 != -1 && isnull(row_identifier3))
  2141. {
  2142. format(SQL::upd_form2, sizeof(SQL::upd_form2), " AND `%s`='%d'", column_where2, row_identifier2);
  2143. strcat(SQL::upd_form, SQL::upd_form2);
  2144. }
  2145. if(!isnull(column_where2) && !isnull(row_identifier3) && row_identifier2 == -1)
  2146. {
  2147. format(SQL::upd_form2, sizeof(SQL::upd_form2), " AND `%s`='%s'", column_where2, row_identifier3);
  2148. strcat(SQL::upd_form, SQL::upd_form2);
  2149. }
  2150. strcat(SQL::upd_form, " ");
  2151. mysql_tquery(connectionHandle, SQL::upd_form, "", "");
  2152. return 1;
  2153. }
  2154. stock SQL::SetStringEntryEx(const table[], const field[], const value[], const column_where[] = "", const row_identifier[] = "", bool:use_real_escape = true, MySQL:connectionHandle = MYSQL_DEFAULT_HANDLE)
  2155. {
  2156. if(!table[0]) return 0;
  2157. if(!field[0]) return 0;
  2158. if(use_real_escape == true)
  2159. {
  2160. //Not using mysql_escape_string as the %e specifier is faster.
  2161. mysql_format(connectionHandle, SQL::upd_form, sizeof(SQL::upd_form), "UPDATE `%s` SET `%s`='%e' WHERE `%s`='%e' ", table, field, value, column_where, row_identifier);
  2162. }
  2163. else
  2164. {
  2165. mysql_format(connectionHandle, SQL::upd_form, sizeof(SQL::upd_form), "UPDATE `%s` SET `%s`='%s' WHERE `%s`='%e' ", table, field, value, column_where, row_identifier);
  2166. }
  2167. mysql_tquery(connectionHandle, SQL::upd_form, "", "");
  2168. return 1;
  2169. }
  2170. stock SQL::SetStringEntryEx2(const table[], const field[], const value[], const column_where[] = "", const row_identifier[] = "", const column_where2[] = "", const row_identifier2[] = "", row_identifier3 = -1, bool:use_real_escape = true, MySQL:connectionHandle = MYSQL_DEFAULT_HANDLE)
  2171. {
  2172. if(!table[0]) return 0;
  2173. if(!field[0]) return 0;
  2174. if(use_real_escape == true)
  2175. {
  2176. //Not using mysql_escape_string as the %e specifier is faster.
  2177. format(SQL::upd_form, sizeof(SQL::upd_form), "UPDATE `%s` SET `%s`='%e' WHERE `%s`='%e'", table, field, value, column_where, row_identifier);
  2178. }
  2179. else
  2180. {
  2181. format(SQL::upd_form, sizeof(SQL::upd_form), "UPDATE `%s` SET `%s`='%s' WHERE `%s`='%e'", table, field, value, column_where, row_identifier);
  2182. }
  2183. if(!isnull(column_where2) && !isnull(row_identifier2) && row_identifier3 == -1)
  2184. {
  2185. format(SQL::upd_form2, sizeof(SQL::upd_form2), " AND `%s`='%e'", column_where2, row_identifier2);
  2186. strcat(SQL::upd_form, SQL::upd_form2);
  2187. }
  2188. if(!isnull(column_where2) && row_identifier3 != -1 && isnull(row_identifier2))
  2189. {
  2190. format(SQL::upd_form2, sizeof(SQL::upd_form2), " AND `%s`='%d'", column_where2, row_identifier3);
  2191. strcat(SQL::upd_form, SQL::upd_form2);
  2192. }
  2193. strcat(SQL::upd_form, " ");
  2194. mysql_tquery(connectionHandle, SQL::upd_form, "", "");
  2195. return 1;
  2196. }
  2197. stock SQL::SetFloatEntry(const table[], const field[], Float:value, const column_where[] = "", row_identifier = -1, MySQL:connectionHandle = MYSQL_DEFAULT_HANDLE)
  2198. {
  2199. if(!table[0]) return 0;
  2200. if(!field[0]) return 0;
  2201. format(SQL::upd_form, sizeof(SQL::upd_form), "UPDATE `%s` SET `%s`='%f' WHERE `%s`='%d' ", table, field, value, column_where, row_identifier);
  2202. mysql_tquery(connectionHandle, SQL::upd_form, "", "");
  2203. return 1;
  2204. }
  2205. stock SQL::SetFloatEntry2(const table[], const field[], Float:value, const column_where[] = "", row_identifier = -1, const column_where2[] = "", row_identifier2 = -1, const row_identifier3[] = "", MySQL:connectionHandle = MYSQL_DEFAULT_HANDLE)
  2206. {
  2207. if(!table[0]) return 0;
  2208. if(!field[0]) return 0;
  2209. format(SQL::upd_form, sizeof(SQL::upd_form), "UPDATE `%s` SET `%s`='%f' WHERE `%s`='%d'", table, field, value, column_where, row_identifier);
  2210. if(!isnull(column_where2) && row_identifier2 != -1 && isnull(row_identifier3))
  2211. {
  2212. format(SQL::upd_form2, sizeof(SQL::upd_form2), " AND `%s`='%d'", column_where2, row_identifier2);
  2213. strcat(SQL::upd_form, SQL::upd_form2);
  2214. }
  2215. if(!isnull(column_where2) && !isnull(row_identifier3) && row_identifier2 == -1)
  2216. {
  2217. format(SQL::upd_form2, sizeof(SQL::upd_form2), " AND `%s`='%s'", column_where2, row_identifier3);
  2218. strcat(SQL::upd_form, SQL::upd_form2);
  2219. }
  2220. strcat(SQL::upd_form, " ");
  2221. mysql_tquery(connectionHandle, SQL::upd_form, "", "");
  2222. return 1;
  2223. }
  2224. stock SQL::SetFloatEntryEx(const table[], const field[], Float:value, const column_where[] = "", const row_identifier[] = "", MySQL:connectionHandle = MYSQL_DEFAULT_HANDLE)
  2225. {
  2226. if(!table[0]) return 0;
  2227. if(!field[0]) return 0;
  2228. mysql_format(connectionHandle, SQL::upd_form, sizeof(SQL::upd_form), "UPDATE `%s` SET `%s`='%f' WHERE `%s`='%e' ", table, field, value, column_where, row_identifier);
  2229. mysql_tquery(connectionHandle, SQL::upd_form, "", "");
  2230. return 1;
  2231. }
  2232. stock SQL::SetFloatEntryEx2(const table[], const field[], Float:value, const column_where[] = "", const row_identifier[] = "", const column_where2[] = "", const row_identifier2[] = "", row_identifier3 = -1, MySQL:connectionHandle = MYSQL_DEFAULT_HANDLE)
  2233. {
  2234. if(!table[0]) return 0;
  2235. if(!field[0]) return 0;
  2236. format(SQL::upd_form, sizeof(SQL::upd_form), "UPDATE `%s` SET `%s`='%f' WHERE `%s`='%e'", table, field, value, column_where, row_identifier);
  2237. if(!isnull(column_where2) && !isnull(row_identifier2) && row_identifier3 == -1)
  2238. {
  2239. format(SQL::upd_form2, sizeof(SQL::upd_form2), " AND `%s`='%e'", column_where2, row_identifier2);
  2240. strcat(SQL::upd_form, SQL::upd_form2);
  2241. }
  2242. if(!isnull(column_where2) && row_identifier3 != -1 && isnull(row_identifier2))
  2243. {
  2244. format(SQL::upd_form2, sizeof(SQL::upd_form2), " AND `%s`='%d'", column_where2, row_identifier3);
  2245. strcat(SQL::upd_form, SQL::upd_form2);
  2246. }
  2247. strcat(SQL::upd_form, " ");
  2248. mysql_tquery(connectionHandle, SQL::upd_form, "", "");
  2249. return 1;
  2250. }
  2251. stock SQL::TableExists(const tablename[], MySQL:connectionHandle = MYSQL_DEFAULT_HANDLE)
  2252. {
  2253. new query[SQL_FORM_LENGTH], Cache:result, rows;
  2254. mysql_format(connectionHandle, query, sizeof(query),"SELECT * FROM `%s` ", tablename);
  2255. result = mysql_query(connectionHandle, query);
  2256. if(mysql_errno() != 0)
  2257. {
  2258. return SQL_Warning("Query could not be completed due to error: %s", query);
  2259. }
  2260. cache_get_row_count(rows);
  2261. cache_delete(result);
  2262. if(rows > 0)
  2263. {
  2264. return 1;
  2265. }
  2266. return 0;
  2267. }
  2268. stock SQL::ExistsTable(const tablename[], MySQL:connectionHandle = MYSQL_DEFAULT_HANDLE)
  2269. {
  2270. new query[85], Cache:result, rows;
  2271. format(query, sizeof(query), "SHOW TABLES LIKE '%s' ", tablename);
  2272. result = mysql_query(connectionHandle, query);
  2273. if(mysql_errno() != 0)
  2274. {
  2275. return SQL_Warning("Query could not be completed due to error: %s", query);
  2276. }
  2277. cache_get_row_count(rows);
  2278. cache_delete(result);
  2279. if(rows > 0)
  2280. {
  2281. return 1;
  2282. }
  2283. return 0;
  2284. }
  2285. stock SQL::CountRows(const tablename[], MySQL:connectionHandle = MYSQL_DEFAULT_HANDLE)
  2286. {
  2287. new query[SQL_FORM_LENGTH], Cache:result, rows;
  2288. format(query, sizeof(query), "SELECT * FROM `%s` ", tablename);
  2289. result = mysql_query(connectionHandle, query);
  2290. if(mysql_errno() != 0)
  2291. {
  2292. return SQL_Warning("Query could not be completed due to error: %s", query);
  2293. }
  2294. cache_get_row_count(rows);
  2295. cache_delete(result);
  2296. return rows;
  2297. }
  2298. stock SQL::RowExistsEx(const table[], const column_where[] = "", const row_identifier[], MySQL:connectionHandle = MYSQL_DEFAULT_HANDLE)
  2299. {
  2300. new query[SQL_FORM_LENGTH], Cache:result, rows;
  2301. mysql_format(connectionHandle, query, sizeof(query),"SELECT * FROM `%s` WHERE `%s`='%e' ", table, column_where, row_identifier);
  2302. result = mysql_query(connectionHandle, query);
  2303. if(mysql_errno() != 0)
  2304. {
  2305. return SQL_Warning("Query could not be completed due to error: %s", query);
  2306. }
  2307. cache_get_row_count(rows);
  2308. cache_delete(result);
  2309. if(rows > 0)
  2310. {
  2311. return rows;
  2312. }
  2313. return 0;
  2314. }
  2315. stock SQL::RowExistsEx2(const table[], const column_where[] = "", const row_identifier[], const column_where2[] = "", const row_identifier2[] = "", row_identifier3 = -1, MySQL:connectionHandle = MYSQL_DEFAULT_HANDLE)
  2316. {
  2317. new query[SQL_FORM_LENGTH], query2[SQL_FORM_LENGTH], Cache:result, rows;
  2318. format(query, sizeof(query), "SELECT * FROM `%s` WHERE `%s`='%e'", table, column_where, row_identifier);
  2319. if(!isnull(column_where2) && !isnull(row_identifier2) && row_identifier3 == -1)
  2320. {
  2321. format(query2, sizeof(query2), " AND `%s`='%e'", column_where2, row_identifier2);
  2322. strcat(query, query2);
  2323. }
  2324. if(!isnull(column_where2) && row_identifier3 != -1 && isnull(row_identifier2))
  2325. {
  2326. format(query2, sizeof(query2), " AND `%s`='%d'", column_where2, row_identifier3);
  2327. strcat(query, query2);
  2328. }
  2329. strcat(query, " ");
  2330. result = mysql_query(connectionHandle, query);
  2331. if(mysql_errno() != 0)
  2332. {
  2333. return SQL_Warning("Query could not be completed due to error: %s", query);
  2334. }
  2335. cache_get_row_count(rows);
  2336. cache_delete(result);
  2337. if(rows > 0)
  2338. {
  2339. return rows;
  2340. }
  2341. return 0;
  2342. }
  2343. stock SQL::DropTable(const table[], MySQL:connectionHandle = MYSQL_DEFAULT_HANDLE)
  2344. {
  2345. new query[SQL_FORM_LENGTH];
  2346. format(query, sizeof(query), "DROP TABLE %s;", table);
  2347. mysql_tquery(connectionHandle, query, "", "");
  2348. return 0;
  2349. }
  2350. stock SQL::RowExists(const table[], const column_where[] = "", row_identifier = -1, MySQL:connectionHandle = MYSQL_DEFAULT_HANDLE)
  2351. {
  2352. new query[SQL_FORM_LENGTH], Cache:result, rows;
  2353. mysql_format(connectionHandle, query, sizeof(query),"SELECT * FROM `%s` WHERE `%s`='%d' ", table, column_where, row_identifier);
  2354. result = mysql_query(connectionHandle, query);
  2355. if(mysql_errno() != 0)
  2356. {
  2357. return SQL_Warning("Query could not be completed due to error: %s", query);
  2358. }
  2359. cache_get_row_count(rows);
  2360. cache_delete(result);
  2361. if(rows > 0)
  2362. {
  2363. return rows;
  2364. }
  2365. return 0;
  2366. }
  2367. stock SQL::RowExists2(const table[], const column_where[] = "", row_identifier = -1, const column_where2[] = "", row_identifier2 = -1, const row_identifier3[] = "", MySQL:connectionHandle = MYSQL_DEFAULT_HANDLE)
  2368. {
  2369. new query[SQL_FORM_LENGTH], query2[SQL_FORM_LENGTH], Cache:result, rows;
  2370. format(query, sizeof(query), "SELECT * FROM `%s` WHERE `%s`='%d'", table, column_where, row_identifier);
  2371. if(!isnull(column_where2) && row_identifier2 != -1 && isnull(row_identifier3))
  2372. {
  2373. format(query2, sizeof(query2), " AND `%s`='%d'", column_where2, row_identifier2);
  2374. strcat(query, query2);
  2375. }
  2376. if(!isnull(column_where2) && !isnull(row_identifier3) && row_identifier2 == -1)
  2377. {
  2378. format(query2, sizeof(query2), " AND `%s`='%s'", column_where2, row_identifier3);
  2379. strcat(query, query2);
  2380. }
  2381. strcat(query, " ");
  2382. result = mysql_query(connectionHandle, query);
  2383. if(mysql_errno() != 0)
  2384. {
  2385. return SQL_Warning("Query could not be completed due to error: %s", query);
  2386. }
  2387. cache_get_row_count(rows);
  2388. cache_delete(result);
  2389. if(rows > 0)
  2390. {
  2391. return rows;
  2392. }
  2393. return 0;
  2394. }
  2395. stock SQL::CountTables(MySQL:connectionHandle = MYSQL_DEFAULT_HANDLE)
  2396. {
  2397. new Cache:result, rows;
  2398. result = mysql_query(connectionHandle, "SHOW TABLES");
  2399. if(mysql_errno() != 0)
  2400. {
  2401. return SQL_Warning("Query could not be completed due to error: %s", "SHOW TABLES");
  2402. }
  2403. cache_get_row_count(rows);
  2404. cache_delete(result);
  2405. return rows;
  2406. }
  2407. stock SQL::AddForeignKey(const o_table[], const f_table[], const o_key[], const f_key[], const db[], SQL::ftypes:on_delete, SQL::ftypes:on_update, MySQL:connectionHandle = MYSQL_DEFAULT_HANDLE)
  2408. {
  2409. new query[256];
  2410. format(query, sizeof(query), "ALTER TABLE `%s` ADD FOREIGN KEY (`%s`) REFERENCES `%s`.`%s`(`%s`) ON DELETE %s ON UPDATE %s;",
  2411. o_table, o_key, db, f_table, f_key, SQL::RetFType(on_delete), SQL::RetFType(on_update));
  2412. mysql_tquery(connectionHandle, query, "", "");
  2413. return 0;
  2414. }
  2415. #undef SQL_INVALID_HANDLE
  2416. #undef SQL_MAX_TABLE_NAME
  2417. #undef SQL_MAX_HANDLES
  2418. #undef SQL_Warning
  2419. #undef SQL_Error