Skip to content

Commit e6088f0

Browse files
committed
Fix table fields. Add loader
1 parent d700361 commit e6088f0

5 files changed

Lines changed: 92 additions & 9 deletions

File tree

package.json

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
{
22
"name": "react-native-sqlite-explorer",
3-
"version": "0.1.10",
3+
"version": "0.1.11",
44
"description": "Explorer for react-native-sqlite-storage library database inside react native app",
55
"source": "./src/index.tsx",
66
"main": "./lib/commonjs/index.js",

src/assets/reload.png

675 Bytes
Loading

src/components/Loader/index.tsx

Lines changed: 57 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,57 @@
1+
import { useEffect, useRef } from 'react';
2+
import { Animated, Easing, View } from 'react-native';
3+
4+
const AnimateState = {
5+
start: 0,
6+
end: 360,
7+
};
8+
9+
const Loader = () => {
10+
const value = useRef(new Animated.Value(AnimateState.start)).current;
11+
const inputRange = [AnimateState.start, AnimateState.end];
12+
const rotate = value.interpolate({
13+
inputRange,
14+
outputRange: [0 + 'deg', 360 + 'deg'],
15+
});
16+
17+
useEffect(() => {
18+
startAnimate();
19+
20+
return () => {
21+
stopAnimate();
22+
};
23+
}, []);
24+
25+
const startAnimate = () => {
26+
Animated.loop(
27+
Animated.sequence([
28+
Animated.timing(value, {
29+
toValue: AnimateState.end,
30+
useNativeDriver: false,
31+
duration: 1000,
32+
easing: Easing.linear,
33+
}),
34+
])
35+
).start();
36+
};
37+
38+
const stopAnimate = () => value.stopAnimation();
39+
40+
return (
41+
<View style={{ flex: 1 }}>
42+
<Animated.Image
43+
source={require('../../assets/reload.png')}
44+
style={{
45+
transform: [{ rotate }],
46+
justifyContent: 'center',
47+
marginTop: 'auto',
48+
marginBottom: 'auto',
49+
marginLeft: 'auto',
50+
marginRight: 'auto',
51+
}}
52+
/>
53+
</View>
54+
);
55+
};
56+
57+
export default Loader;

src/index.tsx

Lines changed: 14 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -6,12 +6,14 @@ import { GlobalStyles } from './styles';
66
import Table from './components/Table';
77
import Select from './components/Select';
88
import DB from './models/DB';
9+
import Loader from './components/Loader';
910

1011
type SQLiteExplorerProps = {
1112
params: DBParamsType;
1213
};
1314

1415
const SQLiteExplorer = ({ params }: SQLiteExplorerProps) => {
16+
const [status, setStatus] = useState<'loading' | ''>('');
1517
const [signatures, setSignatures] = useState<TableSignature[]>([]);
1618
const [tableData, setTableData] = useState<TableSignature | null>(null);
1719
const [select, setSelect] = useState('');
@@ -33,6 +35,8 @@ const SQLiteExplorer = ({ params }: SQLiteExplorerProps) => {
3335
const loadBase = async () => {
3436
if (!DB) return;
3537

38+
setStatus('loading');
39+
3640
try {
3741
await DB.open(params);
3842
setSignatures(
@@ -46,13 +50,19 @@ const SQLiteExplorer = ({ params }: SQLiteExplorerProps) => {
4650
'Не удалось открыть базу данных: ' + error.message
4751
);
4852
}
53+
54+
setStatus('');
4955
};
5056

5157
const queryTableData = async (signature: TableSignature) => {
5258
if (!signature) return;
5359

60+
setStatus('loading');
61+
5462
const signatureWithValues = await DB.insertValuesIntoSignature(signature);
5563
setTableData(signatureWithValues);
64+
65+
setStatus('');
5666
};
5767

5868
const onActionSuccess = async (tableData: TableSignature) => {
@@ -75,9 +85,11 @@ const SQLiteExplorer = ({ params }: SQLiteExplorerProps) => {
7585
/>
7686
</View>
7787

78-
{!!tableData ? (
88+
{status === 'loading' && <Loader />}
89+
{status !== 'loading' && !!tableData && (
7990
<Table tableData={tableData} onActionSuccess={onActionSuccess} />
80-
) : (
91+
)}
92+
{status !== 'loading' && !tableData && (
8193
<Text style={styles.StatusText}>Empty</Text>
8294
)}
8395
</View>

src/models/DB.ts

Lines changed: 20 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -117,22 +117,36 @@ class Database {
117117
getTablesSignature = async (): Promise<TableSignature[] | null> => {
118118
try {
119119
let tableFields = await this.executeSql(`
120-
SELECT * FROM sqlite_master WHERE type='table' AND name NOT LIKE 'sqlite_%' AND name NOT LIKE 'android_%'`);
120+
SELECT * FROM sqlite_master WHERE type='table' AND name NOT LIKE 'sqlite_%' AND name NOT LIKE 'android_%'`);
121121

122122
return formatObject<any>(tableFields).map((i) => {
123123
const createTableFields: string[] = i.sql
124124
.slice(i.sql.indexOf('(') + 1, i.sql.lastIndexOf(')'))
125125
.replace(/\t|\n/g, '')
126+
.replace(/ *\([^)]*\) */g, '')
126127
.split(',');
127128

128129
return {
129130
name: i.name,
130131
fields: createTableFields
131-
.filter((field) => !field.toLowerCase().startsWith('foreign'))
132-
.map((item) => ({
133-
name: item.trim().split(' ')?.[0],
134-
type: item.trim().split(' ').slice(1).join(' '),
135-
})),
132+
.filter((field) => {
133+
const lField = field.toLowerCase().trim();
134+
135+
if (
136+
lField.startsWith('foreign key') ||
137+
lField.startsWith('primary key') ||
138+
lField.startsWith('constraint')
139+
)
140+
return false;
141+
142+
return true;
143+
})
144+
.map((item) => {
145+
return {
146+
name: item.trim().split(' ')?.[0],
147+
type: item.trim().split(' ').slice(1).join(' '),
148+
};
149+
}),
136150
foreignKeys: createTableFields.filter((field) =>
137151
field.toLowerCase().startsWith('foreign')
138152
),

0 commit comments

Comments
 (0)