Advertisement
hhjfdgdfgdfg

OXmysql docs

Mar 21st, 2024
13
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 8.12 KB | None | 0 0
  1. Placeholders
  2. Placeholders allow for query parameters to be safely executed, preventing common SQL injection methods.
  3.  
  4. Parameters can be passed an an array or map (referred to as named named parameters).
  5.  
  6. local identifier = 'license:abc123'
  7. local group = 'admin'
  8.  
  9. MySQL.scalar('SELECT `username` FROM `users` WHERE `identifier` = ? AND `group` = ?', { identifier, group })
  10.  
  11. -- Named placeholders (deprecated)
  12. MySQL.scalar('SELECT `username` FROM `users` WHERE `identifier` = @identifier AND `group` = @group', {
  13. group = group
  14. identifier = identifier
  15. })
  16. These are distinct from prepared statements which are handled by the MySQL server; you can use MySQL.prepare for more optimised and secure queries.
  17.  
  18.  
  19.  
  20. Insert
  21. Inserts a new entry into the database and returns the insert id for the row, if valid.
  22.  
  23. Promise
  24. local id = MySQL.insert.await('INSERT INTO `users` (identifier, firstname, lastname) VALUES (?, ?, ?)', {
  25. identifier, firstName, lastName
  26. })
  27.  
  28. print(id)
  29. Aliases
  30.  
  31. MySQL.Sync.insert
  32. exports.ghmattimysql.executeSync
  33. exports.oxmysql.insert_async
  34. Callback
  35. MySQL.insert('INSERT INTO `users` (identifier, firstname, lastname) VALUES (?, ?, ?)', {
  36. identifier, firstName, lastName
  37. }, function(id)
  38. print(id)
  39. end)
  40. Aliases
  41.  
  42. MySQL.Async.insert
  43. exports.ghmattimysql.execute
  44. exports.oxmysql.insert
  45.  
  46.  
  47.  
  48.  
  49.  
  50. Prepare
  51. Prepare can be used to execute frequently called queries faster and accepts multiple sets of parameters to be used with a single query.
  52.  
  53. Date will not return the datestring commonly used in FiveM
  54. TINYINT 1 and BIT will not return a boolean
  55. You can only use ? value placeholders, ?? column placeholders and named placeholders will throw an error
  56. Unlike rawExecute, the SELECT statement will return a column, row, or array of rows depending on the number of columns and rows selected.
  57.  
  58. Promise
  59. local response = MySQL.prepare.await('SELECT `firstname`, `lastname` FROM `users` WHERE `identifier` = ?', {
  60. identifier
  61. })
  62.  
  63.  
  64. print(json.encode(response, { indent = true, sort_keys = true }))
  65. Aliases
  66.  
  67. exports.oxmysql.prepare_async
  68. Callback
  69. MySQL.prepare('SELECT `firstname`, `lastname` FROM `users` WHERE `identifier` = ?', {
  70. identifier
  71. }, function(response)
  72. print(json.encode(response, { indent = true, sort_keys = true }))
  73. end)
  74. Aliases
  75.  
  76. exports.oxmysql.prepare
  77.  
  78.  
  79.  
  80.  
  81.  
  82. Query
  83. When selecting data, returns all matching rows and columns; otherwise, returns data like insertId, affectedRows, etc.
  84.  
  85. Promise
  86. local response = MySQL.query.await('SELECT `firstname`, `lastname` FROM `users` WHERE `identifier` = ?', {
  87. identifier
  88. })
  89.  
  90. if response then
  91. for i = 1, #response do
  92. local row = response[i]
  93. print(row.firstname, row.lastname)
  94. end
  95. end
  96. Aliases
  97.  
  98. MySQL.Sync.fetchAll
  99. exports.ghmattimysql.execute
  100. exports.oxmysql.query_async
  101. Callback
  102. MySQL.query('SELECT `firstname`, `lastname` FROM `users` WHERE `identifier` = ?', {
  103. identifier
  104. }, function(response)
  105. if response then
  106. for i = 1, #response do
  107. local row = response[i]
  108. print(row.firstname, row.lastname)
  109. end
  110. end
  111. end)
  112. Aliases
  113.  
  114. MySQL.Async.fetchAll
  115. exports.ghmattimysql.execute
  116. exports.oxmysql.query
  117.  
  118.  
  119.  
  120.  
  121.  
  122.  
  123.  
  124.  
  125. RawExecute
  126. rawExecute can be used to execute frequently called queries faster and accepts multiple sets of parameters to be used with a single query.
  127.  
  128. Date will not return the datestring commonly used in FiveM
  129. TINYINT 1 and BIT will not return a boolean
  130. You can only use ? value placeholders, ?? column placeholders and named placeholders will throw an error
  131. Unlike prepare, the SELECT statement will always return an array of rows. When using SELECT, the return value will match query, single, or scalar depending on the number of columns and rows selected.
  132.  
  133. Promise
  134. local response = MySQL.rawExecute.await('SELECT `firstname`, `lastname` FROM `users` WHERE `identifier` = ?', {
  135. identifier
  136. })
  137.  
  138.  
  139. print(json.encode(response, { indent = true, sort_keys = true }))
  140. Aliases
  141.  
  142. exports.oxmysql.rawExecute_async
  143. Callback
  144. MySQL.rawExecute('SELECT `firstname`, `lastname` FROM `users` WHERE `identifier` = ?', {
  145. identifier
  146. }, function(response)
  147. print(json.encode(response, { indent = true, sort_keys = true }))
  148. end)
  149. Aliases
  150.  
  151. exports.oxmysql.rawExecute
  152.  
  153.  
  154.  
  155.  
  156.  
  157.  
  158.  
  159.  
  160.  
  161. Scalar
  162. Returns the first column for a single row.
  163.  
  164. Promise
  165. local firstName = MySQL.scalar.await('SELECT `firstname` FROM `users` WHERE `identifier` = ? LIMIT 1', {
  166. identifier
  167. })
  168.  
  169. print(firstName)
  170. Aliases
  171.  
  172. MySQL.Sync.fetchScalar
  173. exports.ghmattimysql.scalar
  174. exports.oxmysql.scalar_async
  175. Callback
  176. MySQL.scalar('SELECT `firstname` FROM `users` WHERE `identifier` = ? LIMIT 1', {
  177. identifier
  178. }, function(firstName)
  179. print(firstName)
  180. end)
  181. Aliases
  182.  
  183. MySQL.Async.fetchScalar
  184. exports.ghmattimysql.scalar
  185. exports.oxmysql.scalar
  186.  
  187.  
  188.  
  189.  
  190.  
  191.  
  192. Single
  193. Returns all selected columns for a single row.
  194.  
  195. Promise
  196. local row = MySQL.single.await('SELECT `firstname`, `lastname` FROM `users` WHERE `identifier` = ? LIMIT 1', {
  197. identifier
  198. })
  199.  
  200. if not row then return end
  201.  
  202. print(row.firstname, row.lastname)
  203. Aliases
  204.  
  205. exports.oxmysql.single_async
  206. Callback
  207. MySQL.single('SELECT `firstname`, `lastname` FROM `users` WHERE `identifier` = ? LIMIT 1', {
  208. identifier
  209. }, function(row)
  210. if not row then return end
  211.  
  212. print(row.firstname, row.lastname)
  213. end)
  214. Aliases
  215.  
  216. exports.oxmysql.single
  217.  
  218.  
  219.  
  220.  
  221.  
  222. Transaction
  223. A transaction executes multiple queries and commits them only if all succeed.
  224. If one fails, none of the queries are committed.
  225.  
  226. The return value is a boolean, which is the result of the transaction.
  227.  
  228. Specific format
  229. When using this format, you must pass an array containing sets of queries and parameters to the transaction method.
  230. In this case, your queries do not necessarily match and the values are unique to each query.
  231.  
  232. -- You might rename "values" as "parameters" for mysql-async compatibility.
  233. local queries = {
  234. { query = 'INSERT INTO `test` (id) VALUES (?)', values = { 1 }},
  235. { query = 'INSERT INTO `test` (id, name) VALUES (?, ?)', values = { 2, 'bob' }},
  236. }
  237.  
  238. -- You can also pass an array of arrays.
  239. local queries = {
  240. { 'INSERT INTO `test` (id) VALUES (?)', { 1 } },
  241. { 'INSERT INTO `test` (id, name) VALUES (?, ?)', { 2, 'bob' } },
  242. }
  243. Shared format
  244. When using this format, you must pass an array containing queries and a set containing shared parameters to the transaction method.
  245. In this case, your queries do not necessarily match and the values are unique to each query.
  246.  
  247. -- You might rename "values" as "parameters" for mysql-async compatibility.
  248. local queries = {
  249. 'INSERT INTO `test` (id, name) VALUES (@someid, @somename)',
  250. 'SET `name` = @newname IN `test` WHERE `id` = @someid'
  251. }
  252.  
  253. local values = {
  254. someid = 2,
  255. somename = 'John Doe',
  256. newname = 'John Notdoe'
  257. }
  258. Promise
  259. local success = MySQL.transaction.await(queries, values --[[leave nil for specific format]])
  260. print(success)
  261. Aliases
  262.  
  263. MySQL.Sync.transaction
  264. exports.ghmattimysql.transaction
  265. exports.oxmysql.transaction_async
  266. Callback
  267. -- specific
  268. MySQL.transaction(queries, values, function(success)
  269. print(success)
  270. end)
  271.  
  272. -- shared
  273. MySQL.transaction(queries, function(success)
  274. print(success)
  275. end)
  276. Aliases
  277.  
  278. MySQL.Async.transaction
  279. exports.ghmattimysql.transaction
  280. exports.oxmysql.transaction
  281. Transaction Isolation Level
  282. This can be set through the convar mysql_transaction_isolation_level, and is an integer ranging from 1-4.
  283. The default value is 2.
  284.  
  285. Convar Value Result
  286. 1 Repeatable Read
  287. 2 Read Committed
  288. 3 Read Uncommitted
  289. 4 Serializable
  290.  
  291. Update
  292. Returns the number of rows affected by the query.
  293.  
  294. Promise
  295. local affectedRows = MySQL.update.await('UPDATE users SET firstname = ? WHERE identifier = ?', {
  296. newName, identifier
  297. })
  298.  
  299. print(affectedRows)
  300. Aliases
  301.  
  302. MySQL.Sync.execute
  303. exports.ghmattimysql.executeSync
  304. exports.oxmysql.update_async
  305. Callback
  306. MySQL.update('UPDATE users SET firstname = ? WHERE identifier = ?', {
  307. newName, identifier
  308. }, function(affectedRows)
  309. print(affectedRows)
  310. end)
  311. Aliases
  312.  
  313. MySQL.Async.execute
  314. exports.ghmattimysql.execute
  315. exports.oxmysql.update
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement