mirror of
https://github.com/esx-framework/esx_core.git
synced 2026-09-02 11:18:53 +00:00
mysql-async v3.0.1 support
This commit is contained in:
+66
-94
@@ -1,142 +1,114 @@
|
||||
ESX = nil
|
||||
Items = {}
|
||||
local DataStoresIndex = {}
|
||||
local DataStores = {}
|
||||
local SharedDataStores = {}
|
||||
|
||||
|
||||
TriggerEvent('esx:getSharedObject', function(obj) ESX = obj end)
|
||||
|
||||
AddEventHandler('onMySQLReady', function()
|
||||
MySQL.ready(function()
|
||||
local result = MySQL.Sync.fetchAll('SELECT * FROM datastore')
|
||||
|
||||
local result = MySQL.Sync.fetchAll('SELECT * FROM datastore')
|
||||
for i=1, #result, 1 do
|
||||
local name = result[i].name
|
||||
local label = result[i].label
|
||||
local shared = result[i].shared
|
||||
|
||||
for i=1, #result, 1 do
|
||||
local result2 = MySQL.Sync.fetchAll('SELECT * FROM datastore_data WHERE name = @name', {
|
||||
['@name'] = name
|
||||
})
|
||||
|
||||
local name = result[i].name
|
||||
local label = result[i].label
|
||||
local shared = result[i].shared
|
||||
if shared == 0 then
|
||||
|
||||
local result2 = MySQL.Sync.fetchAll(
|
||||
'SELECT * FROM datastore_data WHERE name = @name',
|
||||
{
|
||||
['@name'] = name
|
||||
}
|
||||
)
|
||||
table.insert(DataStoresIndex, name)
|
||||
DataStores[name] = {}
|
||||
|
||||
if shared == 0 then
|
||||
for j=1, #result2, 1 do
|
||||
local storeName = result2[j].name
|
||||
local storeOwner = result2[j].owner
|
||||
local storeData = (result2[j].data == nil and {} or json.decode(result2[j].data))
|
||||
local dataStore = CreateDataStore(storeName, storeOwner, storeData)
|
||||
|
||||
table.insert(DataStoresIndex, name)
|
||||
table.insert(DataStores[name], dataStore)
|
||||
end
|
||||
|
||||
DataStores[name] = {}
|
||||
else
|
||||
|
||||
for j=1, #result2, 1 do
|
||||
local data = nil
|
||||
|
||||
local storeName = result2[j].name
|
||||
local storeOwner = result2[j].owner
|
||||
local storeData = (result2[j].data == nil and {} or json.decode(result2[j].data))
|
||||
if #result2 == 0 then
|
||||
MySQL.Sync.execute('INSERT INTO datastore_data (name, owner, data) VALUES (@name, NULL, \'{}\')', {
|
||||
['@name'] = name
|
||||
})
|
||||
|
||||
local dataStore = CreateDataStore(storeName, storeOwner, storeData)
|
||||
data = {}
|
||||
else
|
||||
data = json.decode(result2[1].data)
|
||||
end
|
||||
|
||||
table.insert(DataStores[name], dataStore)
|
||||
|
||||
end
|
||||
|
||||
else
|
||||
|
||||
local data = nil
|
||||
|
||||
if #result2 == 0 then
|
||||
|
||||
MySQL.Sync.execute(
|
||||
'INSERT INTO datastore_data (name, owner, data) VALUES (@name, NULL, \'{}\')',
|
||||
{
|
||||
['@name'] = name,
|
||||
}
|
||||
)
|
||||
|
||||
data = {}
|
||||
|
||||
else
|
||||
data = json.decode(result2[1].data)
|
||||
end
|
||||
|
||||
local dataStore = CreateDataStore(name, nil, data)
|
||||
SharedDataStores[name] = dataStore
|
||||
|
||||
end
|
||||
|
||||
end
|
||||
local dataStore = CreateDataStore(name, nil, data)
|
||||
SharedDataStores[name] = dataStore
|
||||
|
||||
end
|
||||
end
|
||||
end)
|
||||
|
||||
function GetDataStore(name, owner)
|
||||
for i=1, #DataStores[name], 1 do
|
||||
if DataStores[name][i].owner == owner then
|
||||
return DataStores[name][i]
|
||||
end
|
||||
end
|
||||
for i=1, #DataStores[name], 1 do
|
||||
if DataStores[name][i].owner == owner then
|
||||
return DataStores[name][i]
|
||||
end
|
||||
end
|
||||
end
|
||||
|
||||
function GetDataStoreOwners(name)
|
||||
local identifiers = {}
|
||||
|
||||
local identifiers = {}
|
||||
|
||||
for i=1, #DataStores[name], 1 do
|
||||
table.insert(identifiers, DataStores[name][i].owner)
|
||||
end
|
||||
|
||||
return identifiers
|
||||
for i=1, #DataStores[name], 1 do
|
||||
table.insert(identifiers, DataStores[name][i].owner)
|
||||
end
|
||||
|
||||
return identifiers
|
||||
end
|
||||
|
||||
function GetSharedDataStore(name)
|
||||
return SharedDataStores[name]
|
||||
return SharedDataStores[name]
|
||||
end
|
||||
|
||||
AddEventHandler('esx_datastore:getDataStore', function(name, owner, cb)
|
||||
cb(GetDataStore(name, owner))
|
||||
cb(GetDataStore(name, owner))
|
||||
end)
|
||||
|
||||
AddEventHandler('esx_datastore:getDataStoreOwners', function(name, cb)
|
||||
cb(GetDataStoreOwners(name))
|
||||
cb(GetDataStoreOwners(name))
|
||||
end)
|
||||
|
||||
AddEventHandler('esx_datastore:getSharedDataStore', function(name, cb)
|
||||
cb(GetSharedDataStore(name))
|
||||
cb(GetSharedDataStore(name))
|
||||
end)
|
||||
|
||||
AddEventHandler('esx:playerLoaded', function(source)
|
||||
local _source = source
|
||||
local xPlayer = ESX.GetPlayerFromId(_source)
|
||||
local dataStores = {}
|
||||
|
||||
local _source = source
|
||||
local xPlayer = ESX.GetPlayerFromId(_source)
|
||||
local dataStores = {}
|
||||
for i=1, #DataStoresIndex, 1 do
|
||||
local name = DataStoresIndex[i]
|
||||
local dataStore = GetDataStore(name, xPlayer.identifier)
|
||||
|
||||
for i=1, #DataStoresIndex, 1 do
|
||||
if dataStore == nil then
|
||||
MySQL.Async.execute('INSERT INTO datastore_data (name, owner, data) VALUES (@name, @owner, @data)',
|
||||
{
|
||||
['@name'] = name,
|
||||
['@owner'] = xPlayer.identifier,
|
||||
['@data'] = '{}'
|
||||
})
|
||||
|
||||
local name = DataStoresIndex[i]
|
||||
local dataStore = GetDataStore(name, xPlayer.identifier)
|
||||
dataStore = CreateDataStore(name, xPlayer.identifier, {})
|
||||
table.insert(DataStores[name], dataStore)
|
||||
end
|
||||
|
||||
if dataStore == nil then
|
||||
|
||||
MySQL.Async.execute(
|
||||
'INSERT INTO datastore_data (name, owner, data) VALUES (@name, @owner, @data)',
|
||||
{
|
||||
['@name'] = name,
|
||||
['@owner'] = xPlayer.identifier,
|
||||
['@data'] = '{}'
|
||||
}
|
||||
)
|
||||
|
||||
dataStore = CreateDataStore(name, xPlayer.identifier, {})
|
||||
table.insert(DataStores[name], dataStore)
|
||||
end
|
||||
|
||||
table.insert(dataStores, dataStore)
|
||||
|
||||
end
|
||||
|
||||
xPlayer.set('dataStores', dataStores)
|
||||
table.insert(dataStores, dataStore)
|
||||
end
|
||||
|
||||
xPlayer.set('dataStores', dataStores)
|
||||
end)
|
||||
|
||||
|
||||
Reference in New Issue
Block a user