You've already forked OpenIntegrations
mirror of
https://github.com/Bayselonarrend/OpenIntegrations.git
synced 2025-08-10 22:41:43 +02:00
Доработки SQLite
This commit is contained in:
1
src/addins/sqlite/Cargo.lock
generated
1
src/addins/sqlite/Cargo.lock
generated
@@ -117,6 +117,7 @@ dependencies = [
|
||||
"addin1c",
|
||||
"base64",
|
||||
"rusqlite",
|
||||
"serde",
|
||||
"serde_json",
|
||||
"serde_rusqlite",
|
||||
]
|
||||
|
@@ -17,4 +17,5 @@ addin1c = "0.5.0"
|
||||
rusqlite = { version = "0.32.1", features = ["bundled"]}
|
||||
serde_json = "1.0"
|
||||
serde_rusqlite = "0.36.0"
|
||||
base64 = "0.22.1"
|
||||
base64 = "0.22.1"
|
||||
serde = "1.0.217"
|
@@ -4,6 +4,8 @@ use crate::component;
|
||||
use serde_rusqlite::{to_params};
|
||||
use base64::{engine::general_purpose, Engine as _};
|
||||
|
||||
|
||||
|
||||
pub fn execute_query(
|
||||
client: &mut component::AddIn,
|
||||
query: String,
|
||||
@@ -16,7 +18,7 @@ pub fn execute_query(
|
||||
};
|
||||
|
||||
// Парсинг JSON параметров
|
||||
let parsed_params: Value = match serde_json::from_str(¶ms_json) {
|
||||
let mut parsed_params: Value = match serde_json::from_str(¶ms_json) {
|
||||
Ok(params) => params,
|
||||
Err(e) => {
|
||||
return format!(
|
||||
@@ -26,15 +28,19 @@ pub fn execute_query(
|
||||
}
|
||||
};
|
||||
|
||||
let params_array = match parsed_params.as_array() {
|
||||
let params_array = match parsed_params.as_array_mut() {
|
||||
Some(array) => array,
|
||||
None => {
|
||||
return r#"{"result": false, "error": "Parameters must be a JSON array"}"#.to_string();
|
||||
}
|
||||
};
|
||||
|
||||
process_blobs(params_array);
|
||||
|
||||
let convert = to_params(params_array).unwrap();
|
||||
let convert = match to_params(params_array){
|
||||
Ok(params) => params,
|
||||
Err(e) => {return format!(r#"{{"result": false, "error": "{}"}}"#, e.to_string())}
|
||||
};
|
||||
|
||||
// Определяем тип запроса
|
||||
if query.trim_start().to_uppercase().starts_with("SELECT") {
|
||||
@@ -105,7 +111,7 @@ fn rows_to_json_array(rows: &mut rusqlite::Rows, cols: &Vec<String>) -> String {
|
||||
}
|
||||
}
|
||||
|
||||
json!(json_array).to_string()
|
||||
json!({ "result": json_array }).to_string()
|
||||
}
|
||||
|
||||
fn from_sql_to_json(value: ValueRef) -> Value {
|
||||
@@ -123,4 +129,40 @@ fn from_sql_to_json(value: ValueRef) -> Value {
|
||||
Value::Object(blob_object)
|
||||
},
|
||||
}
|
||||
}
|
||||
|
||||
fn process_blobs(json_array: &mut Vec<Value>) {
|
||||
|
||||
for item in json_array.iter_mut() {
|
||||
|
||||
if let Value::Object(obj) = item {
|
||||
|
||||
// Проверяем, есть ли ключ "blob"
|
||||
if let Some(Value::String(blob_str)) = obj.get("blob") {
|
||||
match general_purpose::STANDARD.decode(blob_str) {
|
||||
Ok(decoded_blob) => {
|
||||
let current_blob = decoded_blob
|
||||
.into_iter()
|
||||
.map(|b| {
|
||||
if u64::from(b) > i64::MAX as u64 {
|
||||
Value::Number(serde_json::Number::from(i64::MAX))
|
||||
} else {
|
||||
Value::Number(serde_json::Number::from(b as i64))
|
||||
}
|
||||
})
|
||||
.collect::<Vec<Value>>();
|
||||
*item = Value::Array(current_blob);
|
||||
}
|
||||
Err(e) => {
|
||||
// Обработка ошибок декодирования
|
||||
*item = Value::String(format!("blob_error: {}", e));
|
||||
}
|
||||
}
|
||||
}
|
||||
} else if let Value::Array(array) = item {
|
||||
// Рекурсивно обрабатываем вложенные массивы
|
||||
process_blobs(array);
|
||||
}
|
||||
}
|
||||
|
||||
}
|
Binary file not shown.
Binary file not shown.
Binary file not shown.
132
src/ru/OPI/src/CommonModules/OPI_SQLite/Module.bsl
Normal file
132
src/ru/OPI/src/CommonModules/OPI_SQLite/Module.bsl
Normal file
@@ -0,0 +1,132 @@
|
||||
// OneScript: ./OInt/core/Modules/OPI_SQLite.os
|
||||
// Lib: SQLite
|
||||
// CLI: sqlite
|
||||
|
||||
// MIT License
|
||||
|
||||
// Copyright (c) 2023 Anton Tsitavets
|
||||
|
||||
// Permission is hereby granted, free of charge, to any person obtaining a copy
|
||||
// of this software and associated documentation files (the "Software"), to deal
|
||||
// in the Software without restriction, including without limitation the rights
|
||||
// to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
|
||||
// copies of the Software, and to permit persons to whom the Software is
|
||||
// furnished to do so, subject to the following conditions:
|
||||
|
||||
// The above copyright notice and this permission notice shall be included in all
|
||||
// copies or substantial portions of the Software.
|
||||
|
||||
// THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
|
||||
// IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
|
||||
// FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
|
||||
// AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
|
||||
// LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
|
||||
// OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
|
||||
// SOFTWARE.
|
||||
|
||||
// https://github.com/Bayselonarrend/OpenIntegrations
|
||||
|
||||
// BSLLS:Typo-off
|
||||
// BSLLS:LatinAndCyrillicSymbolInWord-off
|
||||
// BSLLS:IncorrectLineBreak-off
|
||||
// BSLLS:NumberOfOptionalParams-off
|
||||
// BSLLS:UsingServiceTag-off
|
||||
// BSLLS:LineLength-off
|
||||
|
||||
//@skip-check module-structure-top-region
|
||||
//@skip-check module-structure-method-in-regions
|
||||
//@skip-check wrong-string-literal-content
|
||||
//@skip-check method-too-many-params
|
||||
//@skip-check constructor-function-return-section
|
||||
|
||||
// Раскомментировать, если выполняется OneScript
|
||||
// #Использовать "../../tools"
|
||||
|
||||
#Область ПрограммныйИнтерфейс
|
||||
|
||||
Функция СоздатьПодключение(Знач База = "") Экспорт
|
||||
|
||||
Если Строка(ТипЗнч(База)) = "AddIn.OPI_SQLite.Main" Тогда
|
||||
Возврат База;
|
||||
КонецЕсли;
|
||||
|
||||
OPI_ПреобразованиеТипов.ПолучитьСтроку(База);
|
||||
|
||||
Коннектор = ПодключитьКомпонентуНаСервере("OPI_SQLite");
|
||||
|
||||
Коннектор.Database = База;
|
||||
|
||||
Результат = Коннектор.Connect();
|
||||
Результат = OPI_Инструменты.JsonВСтруктуру(Результат, Ложь);
|
||||
|
||||
Возврат ?(Результат["result"], Коннектор, Результат);
|
||||
|
||||
КонецФункции
|
||||
|
||||
Функция ВыполнитьЗапрос(Знач ТекстЗапроса, Знач Параметры = "", Знач Соединение = "") Экспорт
|
||||
|
||||
OPI_ПреобразованиеТипов.ПолучитьСтроку(ТекстЗапроса);
|
||||
|
||||
Параметры_ = ОбработатьПараметры(Параметры);
|
||||
Коннектор = СоздатьПодключение(Соединение);
|
||||
|
||||
Если ТипЗнч(Коннектор) <> Тип("AddIn.OPI_SQLite.Main") Тогда
|
||||
Возврат Коннектор;
|
||||
КонецЕсли;
|
||||
|
||||
Результат = Коннектор.Execute(ТекстЗапроса, Параметры_);
|
||||
|
||||
Возврат Результат;
|
||||
|
||||
КонецФункции
|
||||
|
||||
#КонецОбласти
|
||||
|
||||
#Область СлужебныеПроцедурыИФункции
|
||||
|
||||
Функция ПодключитьКомпонентуНаСервере(Знач ИмяКомпоненты, Знач Класс = "Main")
|
||||
|
||||
Если OPI_Инструменты.ЭтоOneScript() Тогда
|
||||
ИмяМакета = OPI_Инструменты.КаталогКомпонентOS() + ИмяКомпоненты + ".zip";
|
||||
Иначе
|
||||
ИмяМакета = "ОбщийМакет." + ИмяКомпоненты;
|
||||
КонецЕсли;
|
||||
|
||||
ПодключитьВнешнююКомпоненту(ИмяМакета, ИмяКомпоненты, ТипВнешнейКомпоненты.Native);
|
||||
|
||||
Компонента = Новый("AddIn." + ИмяКомпоненты + "." + Класс);
|
||||
Возврат Компонента;
|
||||
|
||||
КонецФункции
|
||||
|
||||
Функция ОбработатьПараметры(Знач Параметры)
|
||||
|
||||
Если ЗначениеЗаполнено(Параметры) Тогда
|
||||
|
||||
OPI_ПреобразованиеТипов.ПолучитьМассив(Параметры);
|
||||
|
||||
Для Н = 0 По Параметры.ВГраница() Цикл
|
||||
|
||||
ТекущийПараметр = Параметры[Н];
|
||||
|
||||
Если Не OPI_Инструменты.ЭтоПримитивныйТип(ТекущийПараметр) Тогда
|
||||
OPI_ПреобразованиеТипов.ПолучитьСтроку(ТекущийПараметр);
|
||||
КонецЕсли;
|
||||
|
||||
Параметры[Н] = ТекущийПараметр;
|
||||
|
||||
КонецЦикла;
|
||||
|
||||
Параметры_ = OPI_Инструменты.JSONСтрокой(Параметры, , Ложь);
|
||||
|
||||
Иначе
|
||||
|
||||
Параметры_ = "[]";
|
||||
|
||||
КонецЕсли;
|
||||
|
||||
Возврат Параметры_;
|
||||
|
||||
КонецФункции
|
||||
|
||||
#КонецОбласти
|
11
src/ru/OPI/src/CommonModules/OPI_SQLite/OPI_SQLite.mdo
Normal file
11
src/ru/OPI/src/CommonModules/OPI_SQLite/OPI_SQLite.mdo
Normal file
@@ -0,0 +1,11 @@
|
||||
<?xml version="1.0" encoding="UTF-8"?>
|
||||
<mdclass:CommonModule xmlns:mdclass="http://g5.1c.ru/v8/dt/metadata/mdclass" uuid="2d373a84-c3c2-40ee-baa8-e759487fef4a">
|
||||
<name>OPI_SQLite</name>
|
||||
<synonym>
|
||||
<key></key>
|
||||
<value>OPI SQLite</value>
|
||||
</synonym>
|
||||
<server>true</server>
|
||||
<externalConnection>true</externalConnection>
|
||||
<clientOrdinaryApplication>true</clientOrdinaryApplication>
|
||||
</mdclass:CommonModule>
|
@@ -1,4 +1,4 @@
|
||||
// OneScript: ./OInt/tools/Modules/internal/Modules/OPI_Инструменты.os
|
||||
// OneScript: ./OInt/tools/Modules/internal/Modules/OPI_Инструменты.os
|
||||
|
||||
// MIT License
|
||||
|
||||
@@ -1146,6 +1146,14 @@
|
||||
|
||||
КонецФункции
|
||||
|
||||
Функция ЭтоПримитивныйТип(Знач Значение) Экспорт
|
||||
|
||||
Возврат ТипЗнч(Значение) = Тип("Строка")
|
||||
Или ТипЗнч(Значение) = Тип("Число")
|
||||
Или ТипЗнч(Значение) = Тип("Булево")
|
||||
|
||||
КонецФункции
|
||||
|
||||
#КонецОбласти
|
||||
|
||||
#КонецОбласти
|
||||
|
Binary file not shown.
@@ -56,6 +56,7 @@
|
||||
<commonModules>CommonModule.OPI_Ozon</commonModules>
|
||||
<commonModules>CommonModule.OPI_Notion</commonModules>
|
||||
<commonModules>CommonModule.OPI_Slack</commonModules>
|
||||
<commonModules>CommonModule.OPI_SQLite</commonModules>
|
||||
<commonModules>CommonModule.OPI_S3</commonModules>
|
||||
<commonModules>CommonModule.OPI_TCP</commonModules>
|
||||
<commonModules>CommonModule.OPI_Telegram</commonModules>
|
||||
|
Reference in New Issue
Block a user